2016-07-25 60 views
0

我在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 
+0

你只是在显示你的'create'动作..你的'update'动作和你的'book_rooms_params'方法在哪里? –

+0

你有一个customers_controller吗?通常,'<%= form_for @customer do | f | %>'通过客户/ [customer_id](更新)。 –

+1

好的,你需要在':book_rooms_attributes => [:checked_out]'里加':id'。之后,你将能够更新。否则每次您都会收到新的记录。 –

回答

1

在您的客户控制器: 尝试改变你的函数customer_params像:

def customer_params 
    params.require(:customer).permit(:first_name, :last_name, :phone_number, :sex, {:book_rooms => [:checked_out]}) 
end 

更多吃茶看here

1

更改update方法:

def update 
    @customer = Customer.find(params[:customer_id]) 
    if @customer.update_attributes(customer_params) 
    flash[:notice] = "Customer has checked out" 
    redirect_to @customer 
    else 
    ...redirect to edit page with a flash error message ... 
    end 

end 

您还需要修改您的编辑页面。

<%= form_for(:customer, :url => {:action => 'update', :id => @customer.id}, :method => 'PUT') do |f| %>

1

尝试更改URL更新和改变方法patch你会去更新方法。

<%= form_for :customer, url: customer_path(@customer),method: :patch do |f| %> 
    <%= f.fields_for :book_rooms, @customer.book_rooms do |f| %> 
     <%= f.check_box :checked_out %> 
    <% end %> 
<%= f.submit "Enter" %>