I am trying to follow this coderwall guide on Creating a Limited Rail Invitation System .
In my Rails 4 application, I have the following 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
Here is the correspondence between my models and the models from the textbook:
User <=> UserCalendar UserGroupAdministration MembershipInvite Invite
Now I’m in the “Create a new invitation” section:
- The
Invite model has been updated with the before_create and generate_token filters. Invites controller updated with create action.
However, when I look at the calendar editing view to fill out the invitation form, I get the following error:
NoMethodError in CalendarsController
The problem seems to come from the line @invite = @calendar.invites.build .
-----
UPDATE : here is the contents of my invitation model:
class Invite < ActiveRecord::Base belongs_to :calendar belongs_to :sender, :class_name => 'User' belongs_to :recipient, :class_name => 'User' before_create :generate_token def generate_token self.token = Digest::SHA1.hexdigest([self.calendar_id, self.recipient_role, Time.now, rand].join) end end
-----
UPDATE 2 : In this question , the author explains that the problem may come from CanCanCan and Rolify. I do not use these gems, but I use Pundit. I thought it would be useful in the context of my question.
-----
UPDATE 3 : the migration used for the Invite model is also used here:
class CreateInvites < ActiveRecord::Migration def change create_table :invites do |t| t.string :email t.integer :calendar_id t.integer :sender_id t.integer :recipient_id t.string :recipient_role t.string :token t.timestamps null: false end end end
I am wondering if the problem could be caused by t.string :recipient_role , since the role this User exists only in the Administration table for this Calendar : if :recipient_role is automatically interpreted as recipient.role Rails, maybe this causes an error?
-----
UPDATE 4 : here is the content of the CalendarsController:
class CalendarsController < ApplicationController before_action :set_calendar, only: [:show, :edit, :update, :destroy] before_action :authenticate_user!
-----
UPDATE 5 : here are the server logs:
Started GET "/calendars/2/edit" for ::1 at 2015-09-14 11:44:13 -0700 Processing by CalendarsController#edit as HTML Parameters: {"id"=>"2"} Calendar Load (0.1ms) SELECT "calendars".* FROM "calendars" WHERE "calendars"."id" = ? LIMIT 1 [["id", 2]] User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]] Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.3ms) NoMethodError (undefined method `relation_delegate_class' for Invite:Class): app/controllers/calendars_controller.rb:30:in `edit' Rendered /Users/TXC/.rvm/gems/ ruby-2.2.1@global /gems/actionpack-4.2.2/lib/action_dispatch/middleware/templates/rescues/_source.erb (6.0ms) Rendered /Users/TXC/.rvm/gems/ ruby-2.2.1@global /gems/actionpack-4.2.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.8ms) Rendered /Users/TXC/.rvm/gems/ ruby-2.2.1@global /gems/actionpack-4.2.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.7ms) Rendered /Users/TXC/.rvm/gems/ ruby-2.2.1@global /gems/actionpack-4.2.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (68.9ms) Rendered /Users/TXC/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (0.5ms) Rendered /Users/TXC/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.3ms) Rendered /Users/TXC/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.3ms) Rendered /Users/TXC/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.3ms) Rendered /Users/TXC/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (39.3ms) Rendered /Users/TXC/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.4ms) Rendered /Users/TXC/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.4ms) Rendered /Users/TXC/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (94.2ms)
-----
UPDATE 6 : I just realized that I didn’t have
def invite_params params.require(:invite) end
in the Invites controller: could this be causing the problem here?
-----
Any idea what this error message means and how to fix the problem?