I am new in Rails and I am struggling on an apparently easy task. I have a (stripped down) form:
<%= form_for @order, html: { :class => 'form-inline' } do |f| %>
<fieldset>
<legend><%= params[:action].capitalize %> Order</legend>
<table class="table">
<thead>
<tr>
<th><%= f.label :symbol %></th>
<th><%= f.label :price %></th>
</tr>
</thead>
<tbody>
<tr>
<td><%= f.text_field :symbol, class: 'input-small' %></td>
<td><%= f.text_field :price, class: 'input-small' %></td>
</tr>
</tbody>
</table>
<div id='here'></div>
<%= f.submit "Submit", class: "btn btn-primary" %>
</fieldset>
<% end %>
<%= link_to 'Get quote', view_quote_orders_path, remote: true %>
I want to render the quotation in google finance in the div $(#here) when I click on 'Get quote' and when the symbol text field loses focus. I have already written the code to extract the quotation in Ruby.
In routes.rb I added:
resources :orders do
get 'view_quote', on: :collection
end
In order_controller.rb I added:
def view_quote
respond_to do |format|
format.js { render :layout => false }
end
end
and in view_quote.js.erb:
sym = $('.orders #order_symbol').val();
$('#quotes').html("<%=j render 'googlequote', symbol: sym %>");
and in _googlequote.html.erb (where I will put the logic to extract the quotation):
<%= symbol %>
The error is in view_quote.js.erb, because sym is undefined. If I replace the second line with:
$('#quotes').html("<%=j render 'googlequote', symbol: 'just_a_test' %>");
the partial is rendered, but of course I don't need it. How can I pass the javascript variable sym to the partial _googlequote.html.erb? Otherwise, is there a better way to accomplish my objective?