我搜索了很多,这个问题的常见原因是attr_ascessible:model_attributes没有被声明,但我似乎无法让它工作。
查看下面的日志:裁判和:ticket_order值在params散列中,但随后在表中插入为null。 user_id和event_id的外键保存在新记录中,没有任何错误。关于大规模任务的警告使我得到了attr_ascessible声明,尝试了它的不同变化而没有运气。我正在使用设计。
开发日志
Started POST "/events/1" for 127.0.0.1 at 2011-07-14 17:38:16 +0100
Processing by EventsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ddddddjTdnaLKQgZncSDGYt63JA=", "event"=>{"relationship"=>{"event_id"=>"1", "referee"=>"9", "ticket_order"=>"1"}}, "commit"=>"Confirm Purchase", "id"=>"1"}
User Load (20.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 8 LIMIT 1
Event Load (13.1ms) SELECT "events".* FROM "events" WHERE "events"."id" = 1 LIMIT 1
AREL (18.7ms) INSERT INTO "relationships" ("user_id", "event_id", "created_at", "updated_at", "referee", "ticket_order") VALUES (8, 1, '2011-07-14 16:38:16.963351', '2011-07-14 16:38:16.963351', NULL, NULL)
WARNING: Can't mass-assign protected attributes: relationship
[paperclip] Saving attachments.
Redirected to http://localhost:3000/events/1
Completed 302 Found in 588ms
活动
class Event < ActiveRecord::Base
attr_accessible :artist, :venue, :show_info, :date, :doors, :ticket_issue, :ticket_price,
:travel_cost, :accomodation_cost, :hire_cost, :image, :avatar_url, :relationships_attributes, :referee, :ticket_order
has_many :relationships
has_many :users, :through => :relationships
accepts_nested_attributes_for :relationships
事件控制器
def new
@event = Event.new
@users = Relationships.find(:all)
relationship = @event.relationships.build()
end
def create
@event = Event.new(params[:event])
current_user.attend!(@event)
if @event.save
redirect_to @event, :notice => "Successfully created event."
else
render :action => 'new'
end
end
User.rb
def attending?(event)
relationships.find_by_event_id(event)
end
Relationship.rb
class Relationship < ActiveRecord::Base
belongs_to :user
belongs_to :event
attr_accessible :event_id
表单视图
<%= semantic_form_for @event do |form| %>
<%= form.semantic_fields_for :relationship do |builder| %>
<%= builder.hidden_field :event_id, :value => @event.id %>
<%= builder.inputs do %>
<%= builder.input :referee, :as => :select, :collection => @event.users.all %>
<%= builder.input :ticket_order, :as => :number %>
<% end %>
<% end %>
答案 0 :(得分:2)
是的,必须是这样,替换:
form.semantic_fields_for :relationship
使用:
form.semantic_fields_for :relationships