成功下订单后,我正在尝试创建列表对象和约会对象。如果用户专门通过其列表订购约会,则约会对象嵌套在列表对象中。谁能帮我弄清楚我做错了什么?
这是错误:
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
列表控制器更新操作:
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