I have a drop down like this on my page:
我的页面上有这样的下拉列表:
<p>
<%= f.label :episode_id %><br />
<%= f.collection_select(:episode_id, @episodes, :id, :show) %>
</p>
An episode has an id and belongs_to
to a show which has a name. In the dropdown, I'd like to display the show name. :show.name
doesn't work to display the name. How do I do this?
一集有一个id和belongs_to到一个有名字的节目。在下拉列表中,我想显示节目名称。 :show.name无法显示名称。我该怎么做呢?
3 个解决方案
#1
1
One way to do this would be to create a method in your Episode class called show_name
like so:
一种方法是在Episode类中创建一个名为show_name的方法,如下所示:
def show_name
show.name
end
The last symbol you are passing into collection_select is the name of the method that you want to call to get the option text.
传递给collection_select的最后一个符号是要调用以获取选项文本的方法的名称。
#2
0
I don't know if this would work, but did you try episode.show.name
?
我不知道这是否有效,但你试过episode.show.name吗?
#3
0
You could use #select instead of #collection_select. You need to do a bit more work to construct the value/text pairs, but it's not too bad.
您可以使用#select而不是#collection_select。你需要做更多的工作来构造值/文本对,但它并不是太糟糕。
f.select(:episode, :id, @episodes.map{|e| [e.show.name, e.id]})
#1
1
One way to do this would be to create a method in your Episode class called show_name
like so:
一种方法是在Episode类中创建一个名为show_name的方法,如下所示:
def show_name
show.name
end
The last symbol you are passing into collection_select is the name of the method that you want to call to get the option text.
传递给collection_select的最后一个符号是要调用以获取选项文本的方法的名称。
#2
0
I don't know if this would work, but did you try episode.show.name
?
我不知道这是否有效,但你试过episode.show.name吗?
#3
0
You could use #select instead of #collection_select. You need to do a bit more work to construct the value/text pairs, but it's not too bad.
您可以使用#select而不是#collection_select。你需要做更多的工作来构造值/文本对,但它并不是太糟糕。
f.select(:episode, :id, @episodes.map{|e| [e.show.name, e.id]})