Reputation: 87
How can I concatenate a jquery variable into a rails partial call?
Here's an example:
var myAttribute = x;
$(this).html('<%= j render partial: "pages/edit_[**myAttribute**]" %>');
Thanks in advance.
Upvotes: 1
Views: 298
Reputation: 4115
try with this, so you assign the value to @param
<% @param = capture do %>
<%= javascript_tag "document.write(myAttribute);" %>
<% end %>
and then do something like this to use it in the call for your partial.
$(this).html('<%= j render partial: "pages/edit_#{@param}" %>');
Upvotes: 0
Reputation: 2478
You can try this option, let me know if it works
Use .load
instead of html:
$(this).load("/pages/edit_page?attribute=" + myAttribute);
PagesController:
def edit_page
render "pages/edit_#{params[:attribute]}", layout: false
end
routes.rb:
get "pages/edit_page", to: "pages#edit_page"
Upvotes: 1