Rails - 活动登记表

时间:2016-07-22 14:41:46

标签: ruby-on-rails ruby

我正在尝试创建与here解释完全相同的内容。

然而,使用

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

我得到一个'未定义的方法':

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'

我错过了什么?

1 个答案:

答案 0 :(得分:0)

假设您已遵循此

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

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

您需要将registration更改为registrations

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

NoMethodError(未定义的方法`注册&#39;对于#&lt;事件:0x00000003e58bb8&gt;

你的问题本身就有解决方案;)

  你的意思是?注册