Невозможно массово назначить защищенные атрибуты: назначение

Я пытаюсь создать объект списка и объект назначения после успешного размещения заказа. Объект встречи вложен в объект 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

конец

Действие Success содержит следующую форму:

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

ListingModel

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

Действие обновления контроллера списков:

 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

person Benamir    schedule 23.09.2012    source источник


Ответы (1)


Исправьте свою модель Listing с помощью этого

attr_accessible :description, :end_date, :house_id, :order_id, :user_id, :appointment_attributes
accepts_nested_attributes_for :appointment
person Nick Kugaevsky    schedule 23.09.2012
comment
Я все еще получаю ту же ошибку. Любая идея Что еще может быть причиной проблемы? - person Benamir; 23.09.2012
comment
Пожалуйста, добавьте действие обновления вашего контроллера списков (должна быть ошибка). - person Nick Kugaevsky; 23.09.2012
comment
Обновлен @Ник. Я что-то пропустил? - person Benamir; 24.09.2012