简体   繁体   English

下拉选择红宝石导轨

[英]Drop down select ruby rails

Here's my code for a form that contains a drop down list - 这是我的包含下拉列表的表单的代码-

<div class="field">
<%= f.label :type, "Select profile type"%>
<%=
f.select :type, Profile::TYPES,
:prompt => "Select a profile type"
%>
</div>

The drop down menu looks fine. 下拉菜单看起来不错。 But, how would I check which option is selected? 但是,我将如何检查选择了哪个选项? I want to route to a different view based on this selection. 我想根据此选择路由到其他视图。

Thanks in advance! 提前致谢!

The logic of routing to a different view should occur in your controller. 路由到另一个视图的逻辑应该在控制器中发生。 When the user submits this form, check the value of the params, and perform your logic to route to a view: 当用户提交此表单时,请检查params的值,并执行逻辑以路由到视图:

class ExampleController

  def routing
    case params[:example][:type]
    when 'foo'
      redirect_to foo_path
    when 'bar'
      redirect_to bar_path
  end
end

You can create a custom action name, since this routing isn't one of the CRUD operations. 您可以创建自定义操作名称,因为此路由不是CRUD操作之一。 You will need to place this route into the config/routes.rb file if it is a custom name. 如果是自定义名称,则需要将此路由放入config / routes.rb文件。

Optionally, you can bind to the select's onChange event, as mentioned by others to auto-submit the form when the user changes the value. (可选)您可以绑定到select的onChange事件,正如其他人提到的那样,当用户更改值时自动提交表单。 This would still send the data to the controller and perform a redirect. 这仍然会将数据发送到控制器并执行重定向。 The advantage to this approach is that you can keep your route information out of Javascript, and in the Rail's controller. 这种方法的优势在于,您可以将路线信息保留在Javascript之外,也可以保留在Rail的控制器中。

More on Rails routing can be found here: http://guides.rubyonrails.org/routing.html More on Javascript binding to onChange can be found here: http://www.w3schools.com/jsref/event_onchange.asp 有关Rails路由的更多信息,请访问: http : //guides.rubyonrails.org/routing.html有关绑定到onChange的Java语言的更多信息,请访问: http : //www.w3schools.com/jsref/event_onchange.asp

You should be able to use jQuery or any other popular Javascript framework to achieve this -- either attach an onChange listener or set the value somewhere and check it. 您应该能够使用jQuery或任何其他流行的Javascript框架来实现此目的-附加一个onChange侦听器或在某个位置设置值并进行检查。 Events, yay, etc. 事件,等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM