我是Rails中的新成员,我在一个显然很容易的任务中挣扎。 我有一个(精简)形式:将Javascript变量传递给Rails partials
<%= 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 %>
我要呈现在div $在谷歌财经的报价(#这里)当我点击“获取报价”和当符号文本字段失去焦点。 我已经编写了代码来提取Ruby中的报价。
在routes.rb中我加入:
resources :orders do
get 'view_quote', on: :collection
end
在order_controller.rb我加入:
def view_quote
respond_to do |format|
format.js { render :layout => false }
end
end
和在view_quote.js.erb:
sym = $('.orders #order_symbol').val();
$('#quotes').html("<%=j render 'googlequote', symbol: sym %>");
和_googlequote .html.erb(我将把逻辑提取出来):
<%= symbol %>
错误在view_quote.js.erb中,因为sym是未定义的。 如果我更换了与第二行:
$('#quotes').html("<%=j render 'googlequote', symbol: 'just_a_test' %>");
部分呈现,但当然我并不需要它。 如何将javascript变量sym传递给部分_googlequote.html.erb? 否则,有没有更好的方法来实现我的目标?
请参阅http://stackoverflow.com/questions/5161952/rails-link-to-remote-with-params关于如何发送参数作为远程请求的一部分。 –