简体   繁体   English

如何正确使用带红宝石的选择框?

[英]How do I properly use a select box with ruby on rails?

I have a select box for the event types of an event. 我有一个事件的事件类型的选择框。 event belongs to event_type and event_type has many events. 事件属于event_type,而event_type具有许多事件。 Here's what I have for the select box: 这是我选择框的内容:

<%= f.select :event_type, options_from_collection_for_select(EventType.all, :id, :name, @event.id), :placeholder => 'Select an event type' %>

But, the problem is that when I submit it, it submits the id of the event type and not the name of it. 但是,问题在于,当我提交事件时,它会提交事件类型的ID,而不是事件名称。 So, how would I make it submit the name rather than the id? 那么,我如何让它提交名称而不是ID? If you need to see the any more code than just tell me, thanks 如果您需要查看的代码不仅仅是告诉我,谢谢

The second parameter to options_from_collection_for_select is the value that will be submitted with the form. options_from_collection_for_select的第二个参数是将与表单一起提交的值。 You have :id , so change it to :name . 您具有:id ,因此将其更改为:name

http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_from_collection_for_select http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_from_collection_for_select

(but this seems like a strange thing to do - typically you would store the event type ID.) (但这似乎很奇怪-通常您将存储事件类型ID。)

You should pass name in the Value method, if you want to pass the name, 您应该在Value方法中传递名称,如果您想传递名称,

<%= f.select :event_type, options_from_collection_for_select(EventType.all, :name, :name, @event.id), :placeholder => 'Select an event type' %>

Here is the doc for options_from_collection_for_select 这是options_from_collection_for_select的文档

You can use the id after the submit to load the event type again in your controller post action like this: 您可以在提交后使用id来在控制器发布操作中再次加载事件类型,如下所示:

selected_type = EventType.find(params[:event_type]

It is also a good practice to keep database calls to the controller, so please put the EventType.all statement in there and pass it as local or class variable like you did with event . 保留对控制器的数据库调用也是一种好习惯,因此,请在其中放置EventType.all语句,并将其作为本地变量或类变量传递,就像对event所做的那样。

If you really want to pass the name in your form instead of the id, you can replace the :id part in your call to something more like this options_from_collection_for_select(@event_types, :name, :name, @event.event_type.name) . 如果您确实要在表单中传递名称而不是id,则可以在调用中将:id部分替换为类似options_from_collection_for_select(@event_types, :name, :name, @event.event_type.name) Keep in mind that this value should be unique! 请记住,此值应该是唯一的!

The method works like this: 该方法如下所示:

options_from_collection_for_select(collection, value_method, text_method, selected = nil)

So the first parameter contains all the options, the second defines the value within those option objects which are put into the value field of the HTML option (which is being submitted by the form), the third defines the text which is displayed to the user and the final parameter defines the value of the selected entry (in case you are editing an entry for example). 因此,第一个参数包含所有选项,第二个参数定义这些选项对象中的值,这些对象被放入HTML选项的value字段(由表单提交)中,第三个参数定义显示给用户的文本最后一个参数定义所选条目的value (例如,如果您正在编辑条目)。 For the last parameter, you need to use the events' event_type id, or in your case, the name because you set the value of your HTML tag to it. 对于最后一个参数,您需要使用事件的event_type id,或者使用事件名称,因为您将HTML标记的value设置为其。

Use pages like ApiDock or the Rails tutorials to get examples for some of these methods. 使用ApiDock之类的页面或Rails教程来获取其中一些方法的示例。 http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_from_collection_for_select http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_from_collection_for_select

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

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