我想保存schedule_id
在amount
模型。Rails:爲什麼聯想價值無法保存?
雖然我能救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 %>
意見\時間表\ _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
。
UPDATE
後的日誌。
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)
'f.fields_for(:房)做的| r |'對象** R **,但你使用** A **。 'a.hidden_field:schedule_id' – Emu
感謝您的評論,@Emu。我犯了錯字。儘管我使用'r'而不是'a',結果是一樣的。如果你能給我建議,我將不勝感激。 – SamuraiBlue
當您提交表單時,在schedule_params中傳遞了哪些參數?如果你能檢查你能得到一個很好的見解。 – Emu