How to assign a variable the current output of a select tag

时间:2016-04-21 22:28:27

标签: ruby-on-rails ruby ruby-on-rails-4.2

I'm having some trouble with a select tag.

I want to assign a value to a variable depending on which name is shown on the select tag:

<%= select('product', 'name' , @products.map { |s| [s.name, s.id] }, {}, {:class => "form-control"}) %>

The values display correctly, but I want to assign the currently selected value to a variable so that it can be used later on the same page to display content with the relevant details of the selected product.

For example:

@product = currently_displayed_name

Is this possible with Rails and how to do it? Or do I might need to use another resource?

1 个答案:

答案 0 :(得分:1)

支持评论,答案是Rails 没有给你一种方法。根据您在用户更改选择值时需要操作的内容,您可能能够找到它的JavaScript库。

否则,您可以在页面上加载默认变量,然后使用onchange处理程序处理其余变量:

$('#product_name').change(function() {
  // perform actions on the page with javascript
});