Rails 嵌套表单在数据库中插入空属性

发布于 2024-11-24 17:44:48 字数 2842 浏览 1 评论 0原文

我已经搜索了很多,这个问题的常见原因是 attr_ascessible :model_attributes 没有被声明,但我似乎无法让它工作。

查看下面的日志::referee 和 :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.rbRelationship.rb

def attending?(event)
    relationships.find_by_event_id(event)
end

表单

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

I've searched a lot and the common cause of this problem is attr_ascessible :model_attributes not being declared but I can't seem to get it working.

Looking at the Log below :referee, and :ticket_order values are in the params hash but then are inserted as null in the table. Foreign keys for user_id and event_id are saved in a new record without any errors. The warning about mass assignment led me to the attr_ascessible declaration, tried different variations of it without luck. I'm using devise.

Development log

  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

Events

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

Events Controller

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

form view

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光①夏 2024-12-01 17:44:48

是的,一定是这个,将:替换

form.semantic_fields_for :relationship

为:

form.semantic_fields_for :relationships

Yes it must be this, replace:

form.semantic_fields_for :relationship

with:

form.semantic_fields_for :relationships
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文