Thibaud Clement
Thibaud Clement

Reputation: 6897

Rails 4: ArgumentError in Models#edit - First argument in form cannot contain nil or be empty

First, please let me state that I know similar questions have already been asked and I checked them out:

Unfortunately, none of them helped me figure out a solution to my issue.

I am following Jessica Biggs's Creating a Scoped Invitation System for Rails codewall.com tutorial.

Where the author uses the following models:

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

I am using these models:

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

To make things clearer, here is a correspondance between Jessica's models and mine:

Fast forward to Send Invitation Form section, where the tutorial recommends to include the following form:

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

into what would be in my app the Calendars Edit view.

Of course, I have replaced every occurrence of user_group with calendar.

The problem is that now, when I go to this particular view — which used to work — I am now getting the following error:

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

I understand that I need to define @invite in Calendars#Edit but I don't know how.

I tried @invite = Calendar.invites.new and @invite = User.invites.new but none of those worked.

Any idea how I could fix this?

Upvotes: 0

Views: 129

Answers (1)

Raza
Raza

Reputation: 2388

you actually need the desired record @calendar or @user, for initializing its associated records

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

Upvotes: 1

Related Questions