Concatenate jquery variable into Rails partial

谁说胖子不能爱 提交于 2019-12-24 11:23:03

问题


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

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