Rails嵌套具有预定义字段作为复选框的表单 [英] Rails nested forms with pre-defined fields as checkboxes
问题描述
我正在制作一个包含Room
和RoomAttribute
模型的酒店应用程序.这两个模型通过联接表在彼此之间具有many_to_many
关系.每个模型的属性如下:
I'm making a hotel app that includes Room
and RoomAttribute
models. The two models have a many_to_many
relationship between each other through a join table. The attributes for each model are as follows:
-
Room
–room_number
,room_type
(例如豪华"或套房")和price
. -
RoomAttributes
–name
(例如无线互联网",有线电视",浴缸").
Room
–room_number
,room_type
(e.g. "deluxe" or "suite"), andprice
.RoomAttributes
–name
(e.g. "Wireless Internet", "Cable TV", "Bath Tub").
用户将首先创建一组房间属性,以便每次创建新房间时都可以将其选择为复选框.例如,有些房间可能有无线上网,有些则没有. app/views/rooms/new.html.erb
的代码是(我很抱歉使用原始html).
The user will first create a set of room attributes, so that these can be selected as checkboxes every time a new room is created. For example, some rooms may have wireless internet and some don't. The code for app/views/rooms/new.html.erb
is (my apologies for using raw html).
<form action="<%= rooms_path %>" method="post">
<input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>">
<label for="room_number">Room Number:</label>
<input type="text" name="room[room_number]" id="room_number"> <br>
<label for="room_type">Type:</label>
<input type="text" name="room[room_type]" id="room_type"> <br>
<label for="price">Price:</label>
<input type="text" name="room[price]" id="price"> <br>
<label for="room_attributes">Attributes:</label>
<ul>
<% @room_attributes.each do |room_attribute| %>
<li>
<input type="checkbox" name="room[room_attributes_ids][]" value="<%= room_attribute.id %>">
<%= room_attribute.name %>
</li>
<% end %>
</ul>
<input type="submit" value="Submit">
</form>
我正在使用Rails 4,并且想就以下几点寻求建议:
I'm using Rails 4 and I'd like to ask for advice on the following:
- 使用表单助手或 formtastic 设置表单,以便它已经输出了所有预定义的房间属性,用户只需通过复选框选择要包含在会议室中的属性即可.
- 为此编写
RoomController#create
方法,以便将嵌套的RoomAttribute
模型设置为房间属性.我的app/models/room.rb
中是否需要accepts_nested_attributes_for :room_attributes
? -
在这种情况下如何合并强参数.我读过我应该使用
- Set up the form using form helpers or formtastic so that it already outputs all the pre-defined room attributes and users can simply select which attributes to include in the room via checkboxes.
- Write
RoomController#create
method for this so that it sets the nestedRoomAttribute
models as room attributes. Do I needaccepts_nested_attributes_for :room_attributes
in myapp/models/room.rb
? How to incorporate strong parameters in this scenario. I've read that I should use
params.require(:room).permit(:room_number, :room_type, :price, room_attributes_attributes: [:id])
但这对我不起作用.
谢谢! :)
推荐答案
我能够通过简单地阅读Rails 4文档来解决它.我的Room
模型的每个实例都有一个方法room_attribute_ids=
.请注意,Rails将room_attributes
单一化为room_attribute
并在参数后附加了_ids
,而我以前的实现使用了复数形式和:name_of_associated_model_attributes => [:id]
约定.
I was able to solve it by simply digging into the Rails 4 documentation. Every instance of my Room
model has a method room_attribute_ids=
. Notice that Rails singularized room_attributes
to room_attribute
and appended an _ids
for the param, whereas my previous implementation used the pluralized one and the :name_of_associated_model_attributes => [:id]
convention.
因此,我在new.html.erb
中列出了房间属性,如下所示:
Thus I list the room attributes in new.html.erb
as follows:
<label for="room_attributes">Attributes:</label>
<ul>
<% @room_attributes.each do |room_attribute| %>
<li>
<input type="checkbox" name="room[room_attribute_ids][]" value="<%= room_attribute.id %>">
<%= room_attribute.name %>
</li>
<% end %>
</ul>
然后在控制器中,我定义了一个私有方法来为嵌套属性应用强参数:
Then in the controller I defined a private method to apply strong parameters for the nested attribute:
def room_params
params.require(:room).permit(:room_number, :room_type, :price, :room_attribute_ids => [])
end
这篇关于Rails嵌套具有预定义字段作为复选框的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!