Rails:在 ActionView-Helper“collection_select”中预选择一个值

发布于 2024-07-25 19:56:40 字数 704 浏览 9 评论 0原文

我正在尝试获取 ActionView-Helper collection_select 获取将在下拉菜单中预先选择的值。

(:selected in the html-option-hash)

<%= collection_select(:my_object, :my_method, @my_collection, :id, :description_string, {}, {:selected => @my_collection_object.id}) %>

和 (:selected in the option-hash)

<%= collection_select(:my_object, :my_method, @my_collection, :id, :description_string, {:selected => @my_collection_object.id}, {}) %>

似乎都不起作用。

我究竟做错了什么? 有人能帮我解决这个问题吗?

I'm trying to get the ActionView-Helper collection_select to take a value that will be preselected in the dropdown-menu.

Neither (:selected in the html-option-hash)

<%= collection_select(:my_object, :my_method, @my_collection, :id, :description_string, {}, {:selected => @my_collection_object.id}) %>

nor (:selected in the option-hash)

<%= collection_select(:my_object, :my_method, @my_collection, :id, :description_string, {:selected => @my_collection_object.id}, {}) %>

seem to work.

What am I doing wrong? Can anyone help me on this one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不羁少年 2024-08-01 19:56:40

来自 docs

示例用法(选择关联的 Post 实例的 Author@post):

collection_select(:post, :author_id, Author.all, :id, :name_with_initial)

如果 @post.author_id 已经是 1,则将返回

<select name="post[author_id]">
  <option value="">Please select</option>
  <option value="1" selected="selected">D. Heinemeier Hansson</option>
  <option value="2">D. Thomas</option>
  <option value="3">M. Clark</option>
</select>

:您只需确保 @my_object.my_method 返回与可用选项值之一匹配的值。 如果存在匹配,则将选择该选项。

From the docs:

Sample usage (selecting the associated Author for an instance of Post, @post):

collection_select(:post, :author_id, Author.all, :id, :name_with_initial)

If @post.author_id is already 1, this would return:

<select name="post[author_id]">
  <option value="">Please select</option>
  <option value="1" selected="selected">D. Heinemeier Hansson</option>
  <option value="2">D. Thomas</option>
  <option value="3">M. Clark</option>
</select>

So you just need to make sure that @my_object.my_method returns a value that matches one of the available option values. If there's a match then that option will be selected.

笑咖 2024-08-01 19:56:40

根据 文档,如果 @my_object. my_method 与其中一个选项具有相同的值,默认情况下将选择该选项。

相反,您可以尝试使用 options_from_collection_for_selectselect_tag

<%= select_tag 'my_object[my_method]', options_from_collection_for_select(@my_collection, :id, :description_string, @my_collection_object.id) %>

According to the docs, if @my_object.my_method has the same value as one of the options, that one will be selected by default.

Conversely, you could try using options_from_collection_for_select in conjunction with select_tag:

<%= select_tag 'my_object[my_method]', options_from_collection_for_select(@my_collection, :id, :description_string, @my_collection_object.id) %>
荒路情人 2024-08-01 19:56:40

使用 :selected_value

%= collection_select(:my_object, :my_method, @my_collection, :id, :description_string, {}, {:selected_value => @my_collection_object.id}) %>

Use :selected_value

%= collection_select(:my_object, :my_method, @my_collection, :id, :description_string, {}, {:selected_value => @my_collection_object.id}) %>
远山浅 2024-08-01 19:56:40

检查 @my_object.my_method 是否返回 nil。 如果是的话,

如果调用方法返回 nil,则在选项哈希中不包含 :prompt 或 :include_blank 时不会进行任何选择。

除此之外,您可以尝试使用 lambda,就像在 rdoc 示例中那样

{:disabled => lambda {|category| category.archived? }

在您的情况下,这看起来像

{:selected => lambda {|obj| obj.id == @my_collection_object.id }}

Check if @my_object.my_method returns nil. If it does,

If calling method returns nil, no selection is made without including :prompt or :include_blank in the options hash.

Other than that, you can try using lambda, like in the rdoc example

{:disabled => lambda {|category| category.archived? }

In your case this will look like

{:selected => lambda {|obj| obj.id == @my_collection_object.id }}
清醇 2024-08-01 19:56:40

如果您的集合采用 2 值数组的形式,它也适用:

CURRENCIES = [["USD", "$"], ["BRL", "R$"]]

<%= collection_select :thing, :currency, CURRENCIES, :first, :last %>

:first:last 技巧对于这些事情非常有效,而不必为他们定义一个单独的模型。

It also works if your collection is in the form of an array of 2-value arrays:

CURRENCIES = [["USD", "$"], ["BRL", "R$"]]

<%= collection_select :thing, :currency, CURRENCIES, :first, :last %>

The :first and :last trick works quite nicely for these things, without having to define a separate model for them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文