Ruby-on-rails rails - collection_select 选定的值(如果已定义)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4701521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:06:56  来源:igfitidea点击:

rails - collection_select selected value if defined?

ruby-on-railsruby-on-rails-3

提问by AnApprentice

I have the following:

我有以下几点:

<%= f.collection_select :role_id, roles, :id, :name, :prompt => true, :selected => 2 %>

Problem is I only want the selected value of 2 if the value @permission.role_id is nil.

问题是,如果值@permission.role_id 为零,我只想要选定的值 2。

so I tried:

所以我试过:

<%= f.collection_select :role_id, roles, :id, :name, :prompt => true, {:selected => 2 if @permission.role_id.nil?} %> 

but that made Rails angry. How can I make a condition selected value based on if a separate variable is nil or not?

但这让 Rails 很生气。如何根据单独的变量是否为 nil 来创建条件选择值?

Thanks

谢谢

回答by ecoologic

ok I guess I'll feel stupid in 2 mins, but what about

好吧,我想我会在 2 分钟内感到愚蠢,但是呢

<%= f.collection_select :role_id, roles, :id, :name, prompt: true, @permission.role_id ? {} : {selected: 2 } %>

The reason why your solution is not working is that your ifcan return nil, therefor looking something like that:

您的解决方案不起作用的原因是您if可以 return nil,因此看起来像这样:

<%= f.collection_select :role_id, roles, :id, :name, :prompt => true, {nil} %>

Where {nil}is syntax error

{nil}语法错误在哪里

回答by saneshark

While the accepted solution with the ternary operator works, I don't think it is quite as readable as the following solution:

虽然使用三元运算符的公认解决方案有效,但我认为它不如以下解决方案具有可读性:

<%= f.collection_select :role_id, roles, :id, :name, :prompt => true, :selected => @permission.role_id || 2 %>

回答by Andrei S

put this in your helper

把这个放在你的助手里

def selected(permission)
  if permission.role_id.nil?
    return 2
  else
    .....
  end
end

and this in your view

这在你看来

<%= f.collection_select :role_id, roles, :id, :name, :prompt => true, :selected => selected(@permission) %>

回答by Ryan

I found @ecoologic's answer didn't work for me as ruby attempted to interpret the hash as a key for a last argument instead of looking inside the hash for values.

我发现@ecoologic 的答案对我不起作用,因为 ruby​​ 试图将哈希解释为最后一个参数的键,而不是在哈希内部查找值。

The solution was to use a 'splat'. However splats don't seem to work in that inline format, so I used the following:

解决方案是使用“ splat”。然而,splats 似乎不能以这种内联格式工作,所以我使用了以下内容:

<% selected = @permission.role_id ? {} : {selected: 2 } %>
<%= f.collection_select :role_id, roles, :id, :name, prompt: true, **selected %>

回答by lucapette

The problem is that you can't have an if in that position. So a first solution, though a bit ugly, is something like the following:

问题是你不能在那个位置有一个 if 。因此,第一个解决方案虽然有点难看,但类似于以下内容:

<% if @permission.role_id.nil? %>
  <%= f.collection_select :role_id, roles, :id, :name, :prompt => true, {:selected => 2} %>
<% else %>
  <%= f.collection_select :role_id, roles, :id, :name, :prompt => true %>
<% end %>