我有一个event_log
,我正在尝试为event
提交表单时更新状态。当我尝试将其传递给表单时,它会创建event
,但会在控制台中显示Unpermitted parameter: :event_log
。相关代码如下,如果需要任何其他代码,请告诉我。
_event_accept_form.html.erb
<%= form_for :event, url: events_path do |f| %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.hidden_field :title, :value => @event_logs.event.title %>
<%= f.hidden_field :description, :value => @event_logs.event.description %>
<%= f.hidden_field :start_time, :value => @event_logs.event.start_time %>
<%= f.hidden_field :end_time, :value => @event_logs.event.end_time %>
<%= f.hidden_field :sender_id, :value => @event_logs.user_id %>
<%= f.fields_for @event_logs do |event_log| %>
<%= event_log.hidden_field :status, :value => :event_accept %>
<% end %>
<%= f.submit 'Accept', class: 'btn btn-success' %>
<% end %>
event.rb
class Event < ApplicationRecord
belongs_to :user
validates_presence_of :title, :description, :start_time, :end_time, :user_id
has_many :event_logs, dependent: :destroy
accepts_nested_attributes_for :event_logs
end
event_controller.rb ...
def event_params
params.require(:event).permit(:title, :description, :start_time, :end_time, :user_id, :sender_id, { event_logs_attributes: [:id, :status] })
end
event_log_controller.rb
def show
@event = Event.new
@event_logs = EventLog.find(params[:id])
@invitee = User.find(@event_logs.invitee_id)
end
控制台错误
Started POST "/events" for 127.0.0.1 at 2019-01-27 18:37:25 -0600
Processing by EventsController#create as HTML
Parameters {"utf8"=> "✓"," authenticity_token" =>"BRqfTU2Rya8yKS//
Tp2zp6HV6RSD1b82w+5ng/ lzv15Hs/q35lDxNZtxy0yXBaSyPzKeExJpYjBV/DfmwfnK3w==",
"event"=>{"user_id"=>"4", "title"=>"another 2", "description"=>"ihihi",
"start_time"=>"2019-01-22 01:41:00 UTC", "end_time"=>"2019-01-22 03:41:00 UTC",
"sender_id"=>"2", "event_log"=>{"status"=>"event_accept"}}, "commit"=>"Accept"}
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1
ORDER BY "users"."id" ASC LIMIT $2 [["id", 4], ["LIMIT", 1]]
Unpermitted parameter: :event_log
实际上是在event_log
展示视图中调用表单:
<div class="btn-group">
<%= render "events/event_accept_form" %>
</div>
答案 0 :(得分:0)
这里的问题是表单的生成方式(请注意,不允许的参数是event_log,您应该发送event_logs_attributes)。
由于是事件表单,我想您正在向视图发送@event实例变量
<%= form_for :event, url: events_path do |f| %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.hidden_field :title, :value => @event.title %>
<%= f.hidden_field :description, :value => @event.description %>
<%= f.hidden_field :start_time, :value => @event.start_time %>
<%= f.hidden_field :end_time, :value => @event.end_time %>
<%= f.hidden_field :sender_id, :value => @event.user_id %>
<%= f.fields_for :event_logs, @event_log do |event_log| %>
<%= event_log.hidden_field :status, :value => :event_accept %>
<% end %>
<%= f.submit 'Accept', class: 'btn btn-success' %>
<% end %>
控制器
def event_params
params.require(:event).permit(:title, :description, :start_time, :end_time, :user_id, :sender_id, event_logs_attributes: [:id, :status])
end
def show
@event_log = EventLog.find(params[:id])
@event = @event_log.event
@invitee = User.find(@event_log.invitee_id)
end