0
自定义显示的标签我在查看以下grouped_collection_select
:为grouped_collection_select
<%= grouped_collection_select(:classroom, :course_id, @classrooms, :courses, :name, :id, :name) %>
如何自定义显示标签,使得它的两个属性串联?
自定义显示的标签我在查看以下grouped_collection_select
:为grouped_collection_select
<%= grouped_collection_select(:classroom, :course_id, @classrooms, :courses, :name, :id, :name) %>
如何自定义显示标签,使得它的两个属性串联?
grouped_collection_select
将方法名称作为标签的参数。通过在集合中的每个对象上调用该方法来生成标签。
在您的例子,该方法被称为:name
,但你可以创建一个包含两个属性您Classroom
类中的方法,你想如:
# totally made up - use whatever attributes and method-name you want
def name_and_location
[name, location].join(': ')
end
就用它集合选在:
<%= grouped_collection_select(:classroom, :course_id, @classrooms, :courses, :name_and_location, :id, :name) %>
注:上面的例子是该组标签...小屋,你可以同样做个别项目标签相同,只是使用的最后一个参数,并把该方法对你Course
模型来代替。
太棒了!谢谢! – user1175969