0

注文が正常に行われた後、リスト オブジェクトと予約オブジェクトを作成しようとしています。ユーザーが自分のリストで特に予約を注文した場合、予約オブジェクトは Listing オブジェクト内にネストされます。誰かが私が間違っていることを理解するのを手伝ってくれますか?

エラーは次のとおりです。

ActiveModel::MassAssignmentSecurity::Error in ListingsController#update
Can't mass-assign protected attributes: appointment

注文作成アクション:

def create
@order = current_cart.build_order(params[:order])
@order.ip_address = request.remote_ip
@order.user_id = current_user.id


respond_to do |format|
  if @order.save_with_payment

    @order.add_line_items_from_cart(current_cart)         
    Cart.destroy(session[:cart_id])
    session[:cart_id] = nil

    @order.line_items.each do |line_item|
      if line_item.service_id == 2
        listing = Listing.create!(:order_id => @order.id, :user_id => current_user.id, :house_id => @order.house_id)
      end

      if line_item.service_id ==3
        Appointment.create!(:order_id => @order.id, :user_id => current_user.id, :house_id => @order.house_id, :listing_id => listing)
      end
    end

    format.html { render :action => "success", :notice => 'Thank you for your order.' }
    format.xml { render :xml => @order, :status => :created, :location => @order }

  else
    format.html { render :action => "new" }
    format.xml { render :xml => @order.errors,
    :status => :unprocessable_entity }
  end
end

終わり

成功アクションには次の形式が含まれます。

<div class="contain">
<h3>Your order has been received</h3>

<% @order.line_items.each do |line_item| %> 
    <%if line_item.service_id == 2 %>
        <p>Prepare Listing</p>      
        <%= simple_form_for(@order.listing) do |f| %>
            <%= f.error_notification %>

            <div class="form-inputs">

              <%= f.input :start_date %>
              <%= f.input :end_date %>
              <%= f.input :list_price %> <h3>Suggessted list price:****</h3>
              <%= f.input :description %>
              <%= f.input :showing_instructions %>
            </div>


            <% @order.line_items.each do |line_item2| %>
                <%if line_item2.service_id == 3 %>
                    <p>Set up appointments</p>

                    <%= f.simple_fields_for @order.appointment do |a| %>
                        <div class="form-inputs">
                            <%= a.input :start_date %>
                            <%= a.input :showing_instructions%>                         

                            <%= a.input :status %>
                         </div>
                    <% end %>                                             
                <%end%>
            <%end%>

            <div class="form-actions">
              <%= f.button :submit %>
            </div>
        <% end %>
    <%end%>
<%end%>

リストモデル

attr_accessible :description, :end_date, :house_id, :order_id, :user_id, :appointments_attributes
accepts_nested_attributes_for :appointment


  has_one :appointment 
  belongs_to :order

予約モデル

  attr_accessible :start_date, :end_date, :listing_id, :order_id, :user_id, 

  belongs_to :listing
  belongs_to :order
  belongs_to :user

オーダーモデル

  attr_accessible :first_name, :ip_address, :last_name, :cart_id  

  belongs_to :user
  belongs_to :house
  belongs_to :cart

  has_many :transactions, :class_name => "OrderTransaction"
  has_many :line_items, :dependent => :destroy
  has_one :listing
  has_one :appointment

Listings コントローラーの更新アクション:

 def update
@listing = Listing.find(params[:id])


respond_to do |format|
  if @listing.update_attributes(params[:listing])

    format.html { redirect_to @listing, notice: 'Listing was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: "edit" }
    format.json { render json: @listing.errors, status: :unprocessable_entity }
  end
end
  end
4

1 に答える 1

1

Listingこれでモデルを修正します

attr_accessible :description, :end_date, :house_id, :order_id, :user_id, :appointment_attributes
accepts_nested_attributes_for :appointment
于 2012-09-23T17:20:54.753 に答える