我在book_room控制器的創建方法是從它在客戶展示頁面父更新子節點屬性軌
class BookRoomsController < ApplicationController
def create
@customer = Customer.find(params[:customer_id])
@customer_room = @customer.book_rooms.create(book_rooms_params)
flash[:notice] = "Customer has been added to room"
redirect_to customer_path(@customer)
end
def destroy
@customer = Customer.find(params[:customer_id])
@customer_room = @customer.book_rooms.find(params[:id])
@customer_room.destroy
flash[:notice] = "Customer room has been deleted"
redirect_to customer_path(@customer)
end
def edit
@customer = Customer.find(params[:customer_id])
end
def update
@customer = Customer.find(params[:customer_id])
@customer.book_rooms.update(book_rooms_params)
flash[:notice] = "Customer has checked out"
redirect_to @customer
end
private
def book_rooms_params
params.require(:book_room).permit(:room, :first_name, :last_name, :phone_number, :checked_out)
end
末
客戶模型和book_room模型
class Customer < ApplicationRecord
has_many :book_rooms
accepts_nested_attributes_for :book_rooms
end
class BookRoom < ApplicationRecord
belongs_to :customer
end
<%= form_for [@customer, @customer.book_rooms.build] do |f| %>
<% @room = Room.all %>
<%= f.label "Room: "%>
<%= f.select(:room, @room.collect { |a| [a.room_number, a.id] }) %>
<%= f.submit "Enter" %>
<div class="col-md-12"><%= render @customer.book_rooms.order("created_at DESC") %></div>
這很好地工作,所有的子對象被創建。現在,當我嘗試編輯子屬性不會在所有
繼承人在book_room編輯頁面編輯表單更新/動作
<%= form_for @customer do |f| %>
<%= f.fields_for :book_rooms, @customer.book_rooms do |f| %>
<%= f.check_box :checked_out %>
<% end %>
<%= f.submit "Enter" %>
是不是我做錯了什麼?爲什麼不更新?
客戶控制器
class CustomersController < ApplicationController
before_action :set_customer, only: [:show, :edit, :update, :destroy]
# POST /customers.json
def create
@customer = Customer.new(customer_params)
respond_to do |format|
if @customer.save
format.html { redirect_to @customer, notice: 'Customer was successfully created.' }
format.json { render :show, status: :created, location: @customer }
else
format.html { render :new }
format.json { render json: @customer.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @customer.update(customer_params)
format.html { redirect_to @customer, notice: 'Customer was successfully updated.' }
format.json { render :show, status: :ok, location: @customer }
else
format.html { render :edit }
format.json { render json: @customer.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_customer
@customer = Customer.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def customer_params
params.require(:customer).permit(:first_name, :last_name, :phone_number, :sex, :book_rooms_attributes => [:checked_out])
end
你只是在顯示你的'create'動作..你的'update'動作和你的'book_rooms_params'方法在哪裏? –
你有一個customers_controller嗎?通常,'<%= form_for @customer do | f | %>'通過客戶/ [customer_id](更新)。 –
好的,你需要在':book_rooms_attributes => [:checked_out]'里加':id'。之後,你將能夠更新。否則每次您都會收到新的記錄。 –