Unpermitted parameter
。
Althogh有很多类似的问题,我无法找到解决方法。
我想要做的是在创建amounts
时将值保存在schedule
表中。
登录
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
模型
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
控制器
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
查看
/schedules/new.html.erb
...
<%= form_for(@schedule) do |f| %>
<%= render 'schedule_form', f: f %>
<%= f.submit "Create my schedule", class: "btn btn-primary" %>
...
/ schedules / _schedule_form.html.erb
在为<%= room.fields_for(:amounts) do |amount| %>
添加4行之前有效。
...
<%= 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 %>
如果你能给我任何建议,我将不胜感激。
答案 0 :(得分:0)
您尚未关闭room_attributes数组参数。请关闭它并打开数量的属性嵌套参数。还有amount_id属性的数量。
final int[] array = { 1, 2, 3, 1 };
Object proxy = Proxy.newProxyInstance(array.getClass().getClassLoader(), array.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
StringBuilder b=new StringBuilder("the array is");
for(int i:array)
b.append(" ").append(i);
return b.toString();
}
});
System.out.println(proxy.toString());
答案 1 :(得分:0)
从记录的params开始,您的amounts_attributes
是一个对象数组,应该在permit
方法参数中声明:
def schedule_params
params.require(:schedule).permit(
:title, :departure_date,
rooms_attributes: [{
:id, :_destroy, :room, :room_address, :schedule_id, :day,
amounts_attributes: [{
:itinerary_id, :room_id, :event_id, :id, :_destroy, :ccy, :amount
}]
}]
)
end
rooms_attributes
遵循类似的结构,也需要更正。
查看ActionController::Parameters docs中的嵌套参数示例。
答案 2 :(得分:0)
我认为你的参数应该是这样的:
def schedule_params
params.require(:schedule).permit(
:title, :departure_date,
rooms_attributes: [
:id, :_destroy, :room, :room_address, :schedule_id, :day,
amounts_attributes: [ :id, :_destroy,
:itinerary_id, :room_id, :event_id, :id, :_destroy, :ccy, :amount]
]
)
end
因此,对于您的嵌套模型,即使它已经深度嵌套,您仍然需要:id和:_destroy。
请告诉我你的具体情况。