0

I got a situation, there is a Javascript variable I need pass to rails partial view.

For example in test.html.erb

<script type="text/javascript">
var array  =  <%= raw sort_section %>
    for(i = 0; i < array.length; i++) {
        $('#test').append("<%= j render :partial => 'section_in_panel', :locals => {:section => raw array[i]} %>");
    }
</script>

But it keep throwing syntax error, I have try many ways like

{:section => j sort_section_js[i]} %>"
{:section =>" + sort_section_js[i] + "}%>"

I need to use that because I want to call ajax to change array dynamically.

Update

So, maybe I need to write a controller

def get_new_variable
    ...
    return new_variable
end 

Then in the test.html.erb

<script>
$('test').onclick(function(event) {
   //write some ajax call
   //get new_variable
   $('#test').empty();
   $('#test').append("<%= j render :partial => 'section_in_panel', :locals => {:section => raw new_variable} %>");
  });
)</script>

Is that right direction?

5
  • what does sort_section_js hold? Commented Aug 26, 2016 at 7:06
  • It hold a nested array, sorry I forgot to change the variable name. Commented Aug 26, 2016 at 7:08
  • Check this out stackoverflow.com/questions/4959770/… Commented Aug 26, 2016 at 7:12
  • Javascript is run on the client after ruby renders the view on the server, then you can't pass the value of javascript variable to rails partial view. Commented Aug 26, 2016 at 7:14
  • I get it, so the only way is to write a new controller to get new variable, and then pass the new variable to partial view? Commented Aug 26, 2016 at 7:18

2 Answers 2

1

As Alberto Juan wrote in comment, you can't use javascript variable in ruby code. Ruby code will be interpreted before javascript and will not know what is the i.

Update: Your update will not work either as your using new_variable you get in your javascript code.

For using partial after ajax call, follow instructions in this link

Example:

items/index.html.erb

<div class="grid">
  <% @categories.each do |cat| %>
    <%= link_to cat.name, fetch_items_path(:cat_id => cat.id), :remote => true %>
  <% end %>

  <%= render partial: 'item_grid', locals: { items: @items} %>
</div>

routes.rb

get "/fetch_items" => 'items#from_category', as: 'fetch_items'

items_controller.rb

def index
    @items = Item.all
    @categories = Category.all
end

def from_category
    @selected = Item.where(:category_id => params[:cat_id])
    respond_to do |format|
        format.js
    end
end

views/items/from_category.js.erb

$("#items_grid").html("<%= escape_javascript(render partial: 'items_list', locals: { items: @selected } ) %>");  
Sign up to request clarification or add additional context in comments.

1 Comment

Great practice! After I did this tiny practice, I got the right direction to finish my project. Thanks!
0

You can use gem gon or simply do something like this:

<script>
  MyVars[:someVar] = <%= some_value %>;
</script>

But, I recommend you to separate your backend an frontend code and use JSON for passing data to JS.

2 Comments

But it's the other direction. They want to pass data from JS.
Yep. It's my fail.

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.