更新子属性rails

时间:2016-07-25 12:01:02

标签: ruby-on-rails ruby activerecord

我有一个客户模型和book_room模型

class Customer < ApplicationRecord
  has_many :book_rooms
  accepts_nested_attributes_for :book_rooms
end

class BookRoom < ApplicationRecord
  belongs_to :customer
end 
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

在客户展示页面

<%= 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

3 个答案:

答案 0 :(得分:1)

在您的客户控制器中: 尝试更改您的函数customer_params,如:

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

有关详细说明,请参阅here

答案 1 :(得分: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| %>

答案 2 :(得分:1)

尝试更改网址以更新并将方法更改为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" %>
相关问题