Ruby on rails 强参数未以嵌套形式保存
在我的应用程序中,我想创建具有名称和价格的事件 在这些活动中,我希望能够添加参与者,他们有名字和薪水 我用宝石茧做嵌套的形状 从EventsController中,我尝试创建我的参与者 我没有找到正确的方法来做这件事 事件控制器 如果需要,以下是我的模型:Ruby on rails 强参数未以嵌套形式保存,ruby-on-rails,ruby-on-rails-5,simple-form,strong-parameters,Ruby On Rails,Ruby On Rails 5,Simple Form,Strong Parameters,在我的应用程序中,我想创建具有名称和价格的事件 在这些活动中,我希望能够添加参与者,他们有名字和薪水 我用宝石茧做嵌套的形状 从EventsController中,我尝试创建我的参与者 我没有找到正确的方法来做这件事 事件控制器 如果需要,以下是我的模型: class Event < ApplicationRecord has_many :participants, dependent: :destroy accepts_nested_attributes_for :partici
class Event < ApplicationRecord
has_many :participants, dependent: :destroy
accepts_nested_attributes_for :participants, reject_if: :all_blank, allow_destroy: true
end
class Participant < ApplicationRecord
belongs_to :event
end
问题出在我的表格上。。。。 蚕茧需要特殊处理
<div class="container">
<div class="col-xs-12">
<%= simple_form_for @event do |f| %>
<%= f.input :name, label: "Event's name" %>
<%= f.input :total_price, label: "What is the total price" %>
<h2> Add your friends to share the bill</h2>
<div id="participants">
<%= f.simple_fields_for :participants do |participant| %>
<%= render "events/participants_fields", f: participant %>
<% end %>
</div>
<div class="links">
<%= link_to_add_association 'add a friend', f, :participants, partial: "events/participants_fields", class:"btn btn-primary" %>
</div>
<%= f.button :submit %>
<% end %>
</div>
</div>
我像这样清洁控制器:
class EventsController < ApplicationController
def new
@event = Event.new
end
def create
@event = Event.new(params_event)
if @event.save
redirect_to event_path(@event)
end
end
def show
@event = Event.find(params[:id])
end
private
def params_event
params
.require(:event).permit(:name,:total_price, participants_attributes: [:first_name, :salary, :_destroy ] )
end
end
大家好,欢迎来到Stack Overflow。接受嵌套的属性应该只与@event=event.createparams\u事件行一起工作-您不需要以您尝试的方式手动创建参与者。您的许可/要求行中似乎设置了正确的字段名。。。那么,你能告诉我们,当你这么做的时候,你在观察什么吗?您是否收到错误消息?你能看看你的日志文件,告诉我们params和任何未经许可的params的情况吗?谢谢:我已经粘贴了我的日志。。。我没有错误,但没有保存参与者:Cocoon需要特殊的div哦,这很奇怪,有点脆弱:/很高兴听到你解决了它:大约一天后,你可以将自己的答案标记为已接受的答案,这意味着其他人可以看到你的问题已经解决了。。。对于遇到相同问题并怀疑您是否有解决方案的其他人来说,这是很有帮助的:
<%= simple_form_for @event do |f| %>
<%= f.input :name, label: "Event's name" %>
<%= f.input :total_price, label: "What is the total price" %>
<h2> Add your friends to share the bill</h2>
<%= f.simple_fields_for :participants do |participant| %>
<%= render "events/participants_fields", f: participant %>
<% end %>
<%= link_to_add_association 'add a friend', f, :participants, partial: "events/participants_fields", class:"btn btn-primary" %>
<%= f.button :submit %>
<% end %>
<div class='nested-fields participant-background'>
<%= f.input :first_name, label: "Enter your friend's first name" %>
<%= f.input :salary, label: "Enter his/her monthly pay" %>
<%= link_to_remove_association "Remove this friend", f , class: "btn btn-danger btn-xs" %>
</div>
tarted GET "/" for ::1 at 2017-03-13 13:55:20 +0100
ActiveRecord::SchemaMigration Load (7.0ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by EventsController#new as HTML
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Rendering events/new.html.erb within layouts/application
Rendered events/_participants_fields.html.erb (5.8ms)
Rendered events/new.html.erb within layouts/application (98.6ms)
Rendered shared/_navbar.html.erb (2.5ms)
Rendered shared/_flashes.html.erb (0.4ms)
Completed 200 OK in 408ms (Views: 358.0ms | ActiveRecord: 20.9ms)
Started POST "/events" for ::1 at 2017-03-13 13:55:48 +0100
Processing by EventsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"mMuWIZe06ofG6DCafY2EPrJouIcF4YzKKJx6Y9ct5XUZNHqIyvFY4l3dqiEnxC5NhQapvSnFDukgblJnHg+14g==", "event"=>{"name"=>"Rent a van", "total_price"=>"390"}, "commit"=>"Create Event"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.1ms) BEGIN
SQL (11.9ms) INSERT INTO "events" ("name", "total_price", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["name", "Rent a van"], ["total_price", 390], ["created_at", 2017-03-13 12:55:48 UTC], ["updated_at", 2017-03-13 12:55:48 UTC]]
(6.0ms) COMMIT
(0.2ms) BEGIN
(0.2ms) COMMIT
Redirected to http://localhost:3000/events/93
Completed 302 Found in 25ms (ActiveRecord: 18.6ms)
Started GET "/events/93" for ::1 at 2017-03-13 13:55:48 +0100
Processing by EventsController#show as HTML
Parameters: {"id"=>"93"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Event Load (0.4ms) SELECT "events".* FROM "events" WHERE "events"."id" = $1 LIMIT $2 [["id", 93], ["LIMIT", 1]]
Rendering events/show.html.erb within layouts/application
Participant Load (0.5ms) SELECT "participants".* FROM "participants" WHERE "participants"."event_id" = $1 [["event_id", 93]]
Rendered events/show.html.erb within layouts/application (3.1ms)
Rendered shared/_navbar.html.erb (3.3ms)
Rendered shared/_flashes.html.erb (0.8ms)
Completed 200 OK in 41ms (Views: 34.1ms | ActiveRecord: 1.5ms)
<div class="container">
<div class="col-xs-12">
<%= simple_form_for @event do |f| %>
<%= f.input :name, label: "Event's name" %>
<%= f.input :total_price, label: "What is the total price" %>
<h2> Add your friends to share the bill</h2>
<div id="participants">
<%= f.simple_fields_for :participants do |participant| %>
<%= render "events/participants_fields", f: participant %>
<% end %>
</div>
<div class="links">
<%= link_to_add_association 'add a friend', f, :participants, partial: "events/participants_fields", class:"btn btn-primary" %>
</div>
<%= f.button :submit %>
<% end %>
</div>
</div>
class EventsController < ApplicationController
def new
@event = Event.new
end
def create
@event = Event.new(params_event)
if @event.save
redirect_to event_path(@event)
end
end
def show
@event = Event.find(params[:id])
end
private
def params_event
params
.require(:event).permit(:name,:total_price, participants_attributes: [:first_name, :salary, :_destroy ] )
end
end