我正在尝试构建嵌套表单,用户可以在其中为创建的每个邀请添加一个或多个时隙。
在Invitations
视图中,我有_form.html.erb
:
<%= simple_form_for invitation, id: "invitation_form", :remote => true do |f| %>
<div class="col-md-12">
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
<%= f.collection_select(:guest_id, User.all, :id, :full_name, {}, class: "selectpicker", title: "Choose recipient", multiple: true) %>
<br>
<br>
</div>
<br><br>
<div class="col-md-12">
<%= f.collection_select(:type_of_event, @type_evenement, :id, :type_name, {:include_blank => true}, class: "selectpicker", title: "Type", multiple: false) %>
<br>
</div>
<div class="col-md-12">
<br>
<%= f.collection_select(:event_id, @events_list, :id, :title, {:include_blank => true}, class: "selectpicker", title: "Related event (Optional)", multiple: false) %>
<br>
<br>
<%= f.input :title, placeholder: "Title", label: false %>
<%= f.text_area :memo, placeholder: "Start writing", class: "form form-control", rows: 10 %>
<div class="fields">
<%= f.simple_fields_for :invsession do |builder| %>
<%= render 'invitations/invsession_fields', f: builder %>
<% end %>
</div>
<div class="form-actions">
<br>
<%= f.button :submit, class: "btn btn-primary" %>
</div>
</div>
<% end %>
在同一“邀请”视图中,我有部分_invsession_fields.html.erb
<tr>
<td>
<%= f.input_field :_destroy, as: :hidden %>
<%= link_to 'Delete', '#', class: 'remove_record' %>
</td>
<td>
<div class='input-group date' id='datetimepicker2'>
<%= f.text_field :start, :class => "form-control" %>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span><br>
</div>
</td>
<td>
<div class='input-group date' id='datetimepicker3'>
<%= f.text_field :end, :class => "form-control" %>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</td>
</tr>
在我的Invitation controller
中:
def new
@type_evenement = TypeEvenement.all
@invitation = current_user.invitations.build(invitation_params)
@invitation.invsessions.build
end
def invitation_params
params.require(:invitation).permit(:user_id, :guest_id, :event_id, :start, :title, :type_of_event,:memo, :seen, :deleted, :modified, invsessions_attributes: Invsession.attribute_names.map(&:to_sym).push(:_destroy))
end
我的邀请模式:
class Invitation < ApplicationRecord
belongs_to :user
has_many :invsessions, dependent: :destroy
accepts_nested_attributes_for :invsessions, allow_destroy: true
end
和我的入侵模型:
class Invsession < ApplicationRecord
belongs_to :invitation, optional: true
belongs_to :user
end
显示效果很好,但是当我单击“保存”时,它仅保存新邀请,而不保存新的邀请项。我有以下内容:
Processing by InvitationsController#create as JS
Parameters: {"utf8"=>"✓", "invitation"=>{"guest_id"=>["", "1"], "type_of_event"=>"2", "event_id"=>"108", "title"=>"ssss", "memo"=>"ssss", "invsession"=>{"_destroy"=>"", "start"=>"2018-11-02 04:15 am +08:00", "end"=>"2018-11-02 04:15 am +08:00"}}, "commit"=>"Create Invitation", "locale"=>"en-us"}
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
Unpermitted parameters: :guest_id, :invsession
1
(0.1ms) begin transaction
SQL (23.0ms) INSERT INTO "invitations" ("user_id", "guest_id", "event_id", "title", "memo", "created_at", "updated_at", "type_of_event") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["user_id", 1], ["guest_id", 1], ["event_id", 108], ["title", "ssss"], ["memo", "ssss"], ["created_at", "2018-11-02 08:15:37.916159"], ["updated_at", "2018-11-02 08:15:37.916159"], ["type_of_event", 2]]
(25.6ms) commit transaction
(0.1ms) begin transaction
在这一行中,我不知道如何解决Unpermitted parameters: :guest_id, :invsession
是否需要进行一些修改才能使其正常工作?我正在跟踪关于红宝石(070)的教程,该教程似乎很简单。
答案 0 :(得分:0)
这里的问题是,表单没有为嵌套的表单上的输入创建正确的名称。
您在这里有不同的选择,请记住您使用的是simple_form创建输入,因此,请使用simple_form中的帮助器创建输入,它将自动神奇地创建正确的名称
?php
$date1 = date_create("2017-04-15");
$date2 = date_create("2017-05-18");
//difference between two dates
$diff = date_diff($date1,$date2);
//count days
echo 'Days Count - '.$diff->format("%a");
?
为此,您当然需要配置simple_form以使其适合您要输入的结构。
或者在其他情况下,如果您只想让这样的事情出现,请像这样手动设置输入的名称。
<%= f.input :start, input_html: { class: 'form-control' } %>
这将为表单上的参数创建所需的结构,并将其放入 <%= f.text_field :start, :class => "form-control", name: 'invitation[invsession_attributes][start]' %>
内,因为这是您在允许的params方法上使用的结构。
答案 1 :(得分:-1)
您的invitation_params
必须看起来像这样。据此,它将接受guest_id
和invsessions_attributes
作为Array
。
def invitation_params
params.require(:invitation).permit(:user_id, guest_id:[], :type_of_event, :event_id, :title, :memo, :seen, :deleted, :modified, invsessions_attributes: %i[_destroy start end])
end
您正在使用具有nested_attributes
关系的has_many
,因此这些属性也将作为数组接收。
#update 2
你可以试试这个吗
def invitation_params
params.require(:invitation).permit(:user_id, guest_id:[], :type_of_event, :event_id, :title, :memo, :seen, :deleted, :modified, :invsessions_attributes => [:_destroy, :start, :end])
end
#update 3
def invitation_params
params.require(:invitation).permit(:user_id, :type_of_event, :event_id, :title, :memo, :seen, :deleted, :modified, guest_id:[], :invsessions_attributes => [:_destroy, :start, :end])
end
在guest_id:[]
之前放入invsessions_attributes
。
希望这会对您有所帮助。