1

I want to create RoR instance variable in my :javascript_tag and with i want to render my partail also.

'#{@workss}=' + all_filter_record;
console.log('#{@workss}');
html_data = "#{j render :partial => 'other_portfolio', @workss} }";
$("#other_portfolio").html(html_data)

I tried many things but its not working

2 Answers 2

1

Since you mention :javascript I assume you are using haml to render the view.

Normally you would set @works in the controller, then your view code would be

:javascript
  html_data = "#{j render :partial => 'other_portfolio', @works} }";
  $("#other_portfolio").html(html_data)   

Also note, that if you are using this view in reply to a rails json call (e.g. link_to :remote => true, you should use the :plain tag.

If you really really want to set a variable in your view, that is definitely possible, but not inside the :javascript or :plain tag, but you can use it.

Like so:

- works = all_filter_record
:javascript
  html_data = "#{j render :partial => 'other_portfolio', works} }";
  $("#other_portfolio").html(html_data)   

Note while it is possible to set variables in a view, it is considered bad form to have to much code in views, and setting instance variables even more so (setting an instance variable suggests you are going to use it in more views/partials, but then how will you ever know where the data is set??). So if you need an instance variable: set it in the controller.

Sign up to request clarification or add additional context in comments.

Comments

0

Having javascript code directly in the view partial isn't the best approach. Better try to have javascript logic separated in own assets files. Take a look at: https://github.com/gazay/gon

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.