andkjaer
andkjaer

Reputation: 4065

Render partial onclick in rails

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

I have searched google and here but I can't find anything useable.

Thanks

Upvotes: 2

Views: 9514

Answers (1)

Dominic Goulet
Dominic Goulet

Reputation: 8113

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!

Upvotes: 15

Related Questions