Johnny
Johnny

Reputation: 87

Concatenate jquery variable into Rails partial

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

Answers (2)

xploshioOn
xploshioOn

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

kasperite
kasperite

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

Related Questions