how to use link_to with ajax in rails 3.2.1

喜夏-厌秋 提交于 2019-12-31 04:44:06

问题


I m using Rails 3.2.1. how to use link_to with remote=>true

My Method in Controller

def clickme
    @clk = "you click me"
    respond_to do |format|
        format.js { render :layout=>false }
    end
  end

My View
In my new.html.erb file

<%= link_to "click here", {:action=>"clickme"}, {:remote => true, :id=>"clk"} %>

<div id="allclick">
    <%= render :partial => 'goclick'  %>
</div>  


_goclick.html.erb

<%= @clk %>


clickme.js.erb

$("allclick").update("<%= escape_javascript(render(:partial => "goclick")) %>");

On my web-page everything is fine when I click on click here link nothing change. But when I check Firebug console it shows me:

$("allclick").update("you click me");

In My application.js

//= require jquery
//= require jquery_ujs

Help Me :(


回答1:


try using html instead of update:

$("#allclick").html("<%= escape_javascript(render(:partial => "goclick")) %>");



回答2:


Use $("#allclick") not $("allclick"), you missed the "#" ;)



来源:https://stackoverflow.com/questions/9496172/how-to-use-link-to-with-ajax-in-rails-3-2-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!