Unpermitted parameter
wird angezeigt, wenn ich versuche, neue Daten zu erstellen. Althogh gibt es viele ähnliche Fragen, ich kann nicht herausfinden, wie zu lösen ist.Rails4: Unerreichter Parameter mit verschachtelten Attributen
Was ich tun möchte, ist, den Wert in der amounts
Tabelle zu speichern, wenn Sie schedule
erstellen.
log
Processing by SchedulesController#create as HTML
Parameters: {"utf8"=>"?", "authenticity_token"=>"xxx", "schedule"=>{"title"=>"test title", "departure_date"=>"2016-07-06", "rooms_attributes"=>{"0"=>{"schedule_id"=>"", "amounts_attributes"=>{"0"=>{"schedule_id"=>"", "room_id"=>""}}}}}, "commit"=>"Create my schedule"}
Parameters: {"utf8"=>"?", "authenticity_token"=>"xxx", "schedule"=>{"title"=>"test title", "departure_date"=>"2016-07-06", "rooms_attributes"=>{"0"=>{"schedule_id"=>"", "amounts_attributes"=>{"0"=>{"schedule_id"=>"", "room_id"=>""}}}}}, "commit"=>"Create my schedule"}
User Load (120.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
User Load (120.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Unpermitted parameter: amounts_attributes
Modelle
schedule.rb
class Schedule < ActiveRecord::Base
belongs_to :user
has_many :rooms, inverse_of: :schedule, dependent: :destroy
has_many :amounts, inverse_of: :schedule, dependent: :destroy
accepts_nested_attributes_for :rooms, allow_destroy: true
accepts_nested_attributes_for :amounts, allow_destroy: true
rooms.rb
class Room < ActiveRecord::Base
belongs_to :schedule, inverse_of: :rooms
has_many :events, inverse_of: :room, dependent: :destroy
has_many :amounts, inverse_of: :room, dependent: :destroy
accepts_nested_attributes_for :events, allow_destroy: true
accepts_nested_attributes_for :amounts, allow_destroy: true
amount.rb
class Amount < ActiveRecord::Base
belongs_to :schedule, inverse_of: :amounts
belongs_to :room, inverse_of: :amounts
belongs_to :event, inverse_of: :amounts
end
Controller
schedule_controller.rb
def new
@schedule = Schedule.new
room = @schedule.rooms.build
room.amounts.build
end
def create
@schedule = current_user.schedules.build(schedule_params)
if @schedule.save
flash[:success] = "schedule created!"
redirect_to schedule_path(@schedule)
else
render 'new'
end
end
...
def schedule_params
params.require(:schedule).permit(
:title, :departure_date,
rooms_attributes: [
:id, :_destroy, :room, :room_address, :schedule_id, :day,
amounts_attributes: [
:schedule_id, :room_id, :event_id, :id, :_destroy, :ccy, :amount
]
]
)
end
Ansicht
/schedules/new.html.erb
...
<%= form_for(@schedule) do |f| %>
<%= render 'schedule_form', f: f %>
<%= f.submit "Create my schedule", class: "btn btn-primary" %>
...
/Pläne/_schedule_form.html.erb
Es funktioniert vor für <%= room.fields_for(:amounts) do |amount| %>
4 Zeilen hinzufügen.
...
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control' %>
...
<%= f.fields_for(:rooms) do |room| %>
<%= room.hidden_field :schedule_id %>
<%= room.fields_for(:amounts) do |amount| %> # my app works before add these 4 lines.
<%= amount.hidden_field :schedule_id %> #
<%= amount.hidden_field :room_id %> #
<% end %> #
<% end %>
Es wäre zu schätzen, wenn Sie mir einen Vorschlag geben könnten.
Sie haben keine a: schedule_id in Ihren starken params für Mengen_Attributen, aber Sie übergeben es als param – hypern
Vielen Dank für Ihren Kommentar, @hypern. Ich konnte nicht kopieren und einfügen. Ich aktualisiere starke Parameter in 'schedule_controller.rb'. – SamuraiBlue