【问题标题】:Rails 4: ArgumentError in Models#edit - First argument in form cannot contain nil or be emptyRails 4:Models#edit 中的 ArgumentError - 表单中的第一个参数不能包含 nil 或为空
【发布时间】:2015-08-28 22:50:44
【问题描述】:

首先,请让我声明,我知道已经有人问过类似的问题,并且我检查了它们:

不幸的是,他们都没有帮助我找到解决问题的方法。

我正在关注 Jessica Biggs 的 Creating a Scoped Invitation System for Rails codewall.com 教程。

作者使用以下模型的地方:

class User < ActiveRecord::Association
   has_many :memberships
   has_many :user_groups, through: :memberships
   has_many :invitations, :class_name => "Invite", :foreign_key => 'recipient_id'
   has_many :sent_invites, :class_name => "Invite", :foreign_key => 'sender_id'
end

class UserGroup < ActiveRecord::Association #(Could be a company, club, circle, etc.)
    has_many :memberships
    has_many :users, through: :memberships
    has_many :invites 
end

class Membership <  ActiveRecord::Association #(Pass through model)
    belongs_to :user
    belongs_to :user_group
end

class Invite < ActiveRecord::Base
  belongs_to :user_group
  belongs_to :sender, :class_name => 'User'
  belongs_to :recipient, :class_name => 'User'
end

我正在使用这些模型:

class User < ActiveRecord::Base
  has_many :administrations
  has_many :calendars, through: :administrations
  has_many :invitations, :class_name => "Invite", :foreign_key => 'recipient_id'
  has_many :sent_invites, :class_name => "Invite", :foreign_key => 'sender_id'
end

class Calendar < ActiveRecord::Base
  has_many :administrations
  has_many :users, through: :administrations
  has_many :invites
end

class Administration < ActiveRecord::Base
  belongs_to :user
  belongs_to :calendar
end

class Invite < ActiveRecord::Base
  belongs_to :calendar
  belongs_to :sender, :class_name => 'User'
  belongs_to :recipient, :class_name => 'User'
end

为了让事情更清楚,这里是杰西卡的模型和我的模型之间的对应关系:

  • 用户用户
  • 用户组 日历
  • 成员身份管理

快进到发送邀请表部分,本教程建议在其中包含以下表单:

<%= form_for @invite , :url => invites_path do |f| %>
    <%= f.hidden_field :user_group_id, :value => @invite.user_group_id %>
    <%= f.label :email %>
    <%= f.email_field :email %>
    <%= f.submit 'Send' %>
<% end %>

进入我的应用中的日历编辑视图。

当然,我已将每次出现的user_group 替换为calendar

问题是,现在,当我转到这个以前可以工作的特定视图时,我现在收到以下错误:

ArgumentError in Calendars#edit
First argument in form cannot contain nil or be empty

我知道我需要在 Calendars#Edit 中定义@invite,但我不知道如何。

我尝试了@invite = Calendar.invites.new@invite = User.invites.new,但都没有奏效。

知道如何解决这个问题吗?

【问题讨论】:

    标签: ruby-on-rails forms ruby-on-rails-4 model-view-controller


    【解决方案1】:

    您实际上需要@calendar 或@user 所需的记录来初始化其关联记录

    @calendar = Calendar.find(params[:id])
    @invite = @calendar.invites.build
    

    【讨论】:

    • 非常感谢您的帮助。除非我犯了错误,否则我似乎仍然会遇到同样的错误。
    • 是的,抱歉,我刚刚更新了邀请其他模型之间的关系关联的问题。
    • 你还需要在日历模型中定义邀请关系
    • 其实在Calendar和User中都需要定义has_many: invites
    • 对不起,我没有完全更新我的问题。现在,您可以看到 User 和 Calendar 与 Invite 的关系。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    相关资源
    最近更新 更多