问题
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.
回答1:
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"
回答2:
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}" %>');
来源:https://stackoverflow.com/questions/53490562/concatenate-jquery-variable-into-rails-partial