Ruby on rails Can';t批量分配受保护的属性:约会
我试图在成功下订单后创建一个列表对象和一个约会对象。如果用户特别订购带有其列表的约会,则约会对象嵌套在列表对象中。谁能帮我找出我做错了什么 以下是错误:Ruby on rails Can';t批量分配受保护的属性:约会,ruby-on-rails,nested-forms,nested-attributes,mass-assignment,Ruby On Rails,Nested Forms,Nested Attributes,Mass Assignment,我试图在成功下订单后创建一个列表对象和一个约会对象。如果用户特别订购带有其列表的约会,则约会对象嵌套在列表对象中。谁能帮我找出我做错了什么 以下是错误: ActiveModel::MassAssignmentSecurity::Error in ListingsController#update Can't mass-assign protected attributes: appointment 订单创建操作: def create @order = current_cart.build_o
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
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
结束
成功操作包含以下表单:
<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 :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 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
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
使用此选项修复您的
列表
模型
attr_accessible :description, :end_date, :house_id, :order_id, :user_id, :appointment_attributes
accepts_nested_attributes_for :appointment
我仍然会犯同样的错误。你知道还有什么可能导致这个问题吗?请添加你的房源控制器的更新操作(应该有错误)。Updated@Nick。我错过什么了吗?