我想将schedule_id
保存在amount
模型中。
虽然我可以保存room_id
,但我无法保存schedule_id
。
没有错误。
schema.rb
create_table "amounts", force: :cascade do |t|
t.integer "schedule_id"
t.integer "room_id"
t.integer "event_id"
t.integer "ccy"
t.decimal "amount"
模型\ schedule.rb
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
模型\ room.rb
belongs_to :schedule, inverse_of: :rooms
has_many :amounts, inverse_of: :room, dependent: :destroy
accepts_nested_attributes_for :amounts, allow_destroy: true
模型\ amount.rb
belongs_to :schedule, inverse_of: :amounts
belongs_to :room, inverse_of: :amounts
belongs_to :event, inverse_of: :amounts
schedules_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 root_url
else
render 'new'
end
end
private
def schedule_params
params.require(:schedule).permit(
:title, :departure_date,
rooms_attributes: [
:id, :_destroy, :room, :schedule_id, :day
amounts_attributes: [
:id, :_destroy, :schedule_id, :room_id, :event_id, :ccy, :amount
]
]
)
end
视图\时间表\ new.html.erb
<%= form_for(@schedule) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= render 'schedule_form', f: f %>
<%= f.submit "Create my schedule", class: "btn btn-primary" %>
<br>
<% end %>
views \ schedules \ _schedule_form.html.erb
room_id
可以保存,但schedule_id
无法保存。
<%= f.fields_for(:rooms) do |r| %>
<%= r.hidden_field :schedule_id %>
<%= r.fields_for(:amounts) do |am| %>
<%= am.hidden_field :schedule_id %>
<%= am.hidden_field :room_id %>
<% end %>
<% end %>
如果您能告诉我如何保存schedule_id
,我们将不胜感激。
更新
发布日志。
Processing by SchedulesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxx", "schedule"=>{"title"=>"test", "departure_date"=>"2016-08-28", "rooms_attributes"=>{"0"=>{"schedule_id"=>"", "amounts_attributes"=>{"0"=>{"schedule_id"=>"", "room_id"=>""}}}}}, "commit"=>"Create my schedule"}
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxx", "schedule"=>{"title"=>"test", "departure_date"=>"2016-08-28", "rooms_attributes"=>{"0"=>{"schedule_id"=>"", "amounts_attributes"=>{"0"=>{"schedule_id"=>"", "room_id"=>""}}}}}, "commit"=>"Create my schedule"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
(0.2ms) BEGIN
(0.2ms) BEGIN
SQL (7.0ms) INSERT INTO "schedules" ("title", "departure_date", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["title", "test"], ["departure_date", "2016-08-28"], ["user_id", 1], ["created_at", "2016-08-28 06:08:33.717879"], ["updated_at", "2016-08-28 06:08:33.717879"]]
SQL (7.0ms) INSERT INTO "schedules" ("title", "departure_date", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["title", "test"], ["departure_date", "2016-08-28"], ["user_id", 1], ["created_at", "2016-08-28 06:08:33.717879"], ["updated_at", "2016-08-28 06:08:33.717879"]]
SQL (0.7ms) INSERT INTO "rooms" ("schedule_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["schedule_id", 85], ["created_at", "2016-08-28 06:08:33.727435"], ["updated_at", "2016-08-28 06:08:33.727435"]]
SQL (0.7ms) INSERT INTO "rooms" ("schedule_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["schedule_id", 85], ["created_at", "2016-08-28 06:08:33.727435"], ["updated_at", "2016-08-28 06:08:33.727435"]]
SQL (0.4ms) INSERT INTO "amounts" ("room_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["room_id", 105], ["created_at", "2016-08-28 06:08:33.730019"], ["updated_at", "2016-08-28 06:08:33.730019"]]
SQL (0.4ms) INSERT INTO "amounts" ("room_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["room_id", 105], ["created_at", "2016-08-28 06:08:33.730019"], ["updated_at", "2016-08-28 06:08:33.730019"]]
(5.6ms) COMMIT
(5.6ms) COMMIT
Redirected to https://xxx/schedules/85
Redirected to https://xxx/schedules/85
Completed 302 Found in 197ms (ActiveRecord: 14.2ms)
Completed 302 Found in 197ms (ActiveRecord: 14.2ms)
答案 0 :(得分:1)
要在$ git --version
git version 2.10.0
模型中保存schedule_id
,您需要通过回调手动设置Amount
值。
scheudle_id
这是因为您使用class Amount < ActiveRecord::Base
belongs_to :room
belongs_to :schedule
before_save do
self.schedule_id = room.schedule_id
end
end
表单及其Schedule
和rooms
属于此特定会议室,但amounts
无效由于未使用与Amount
的直接关联,因此会收到通知。
答案 1 :(得分:0)
Remove reference keys from the private method,
private
def schedule_params
params.require(:schedule).permit(
:title, :departure_date,
rooms_attributes: [
:day,
amounts_attributes: [
:ccy, :amount
]
]
)
end
Remove all hidden fields from view files.