开发者

Render partial onclick in rails

开发者 https://www.devze.com 2023-03-21 18:03 出处:网络
Is it possible to render a partial when a link is clicked? I have sear开发者_运维问答ched google and here but I can\'t find anything useable.

Is it possible to render a partial when a link is clicked?

I have sear开发者_运维问答ched google and here but I can't find anything useable.

Thanks


Yes it is possible. The following example uses jQuery, as you tagged it :

In your view file (the page that is actually displayed) :

<%= link_to "Display a new view", path_to_controller, :remote => true %>

In your controller action (path_to_controller), add a js response :

respond_to do |format|
  format.js
end

And in the path_to_controller.js.erb (the js response file) :

$("#your-placeholder-id").prepend('<%= escape_javascript(render 'path/to/view') %>');

Hope that helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消