Rook Rook - 4 months ago 24
Ruby Question

Rails - Event Registration Form

I am trying to create exactly the same thing explained here.

However, using

def new
@event = Event.find params[:event_id]
@registration = @event.registration.new
end


I get an 'undefined method':

Started GET "/events/1/registrations/new" for ::1 at 2016-07-22 13:59:08 +0200
ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by RegistrationsController#new as HTML
Parameters: {"event_id"=>"1"}
Event Load (0.2ms) SELECT "events".* FROM "events" WHERE "events"."id" = ? LIMIT 1 [["id", 1]]
Completed 500 Internal Server Error in 30ms (ActiveRecord: 0.5ms)

NoMethodError (undefined method `registration' for #<Event:0x00000003e58bb8>
Did you mean? registrations
registrations=):
app/controllers/registrations_controller.rb:4:in `new'


What am I missing?

Answer

Assuming you have followed this

#app/models/event.rb
class Event < ActiveRecord::Base
   has_many :registrations
end

#app/models/registration.rb
class Registration < ActiveRecord::Base
   belongs_to :event
end

You need to change registration to registrations

def new
  @event = Event.find params[:event_id]
  @registration = @event.registrations.new
end

NoMethodError (undefined method `registration' for #< Event:0x00000003e58bb8>

You have solution in your question itself ;)

Did you mean? registrations