weixin_33709219 2011-06-03 14:53 采纳率: 0%
浏览 54

Rails jQuery和Ajax?

I'm a rails newbie and I cant seem to get jquery with ajax to work. I have installed the jquery-rails and done rails g jquery:install. Then I did a sample test by calling an alert in the application.js file and it worked. Then I created a controller action 'index' and made a link to it on one of my other pages. I thought to get ajax to work with it, I would simply create a index.js.erb file. However I then got an error saying template missing, so in my controllers' index action, I put the following code:

respond_to do |format|
 format.html
 format.js
end

However, I am not getting anything displayed on my page. In my index.js.erb file I have a simple alert message to test out if its working, and I cannot get that to even come up. Any suggestions? I don't know if it matters but I am using rails 3.0.7. Thanks in advance

  • 写回答

2条回答 默认 最新

  • H_MZ 2011-06-03 16:43
    关注

    Just wrote this using rails 3.0.7, jquery plugin should be compatible with rjs syntax

    routes.rb

      get "main/index"
      get "main/hello"
    

    main_controller.br

    class MainController < ApplicationController
      def index
      end
    
      def hello
      end
    
    end
    

    hello.js.rjs

    page.alert("test");
    

    index.html.erb

    <%= link_to 'test', main_hello_path, :remote => true %>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了