繁体   English   中英

使用belongs_to / has_many关联时未定义的方法“ event_id”

[英]undefined method `event_id' when using a belongs_to/has_many association

我正在创建一个有事件的网站。 每个事件都充当图库并具有has_many图像。 每个图像belongs_to和事件。

我关注了RailsCast#253 CarrierWave宝石。 当我尝试添加新图像时,它说

#图片:0x7302438的未定义方法`event_id'

<%= form_for @image, :html => {:multipart => true} do |f| %>

<%= f.error_messages %>

<%= f.hidden_field :event_id %>

<%= f.label :title %><br />

<%= f.text_field :title %>

这是我的image.rb

class Image < ActiveRecord::Base

attr_accessible :event_id, :title, :image

validates :title, :image, :presence => :true, :uniqueness => :true

belongs_to :event

mount_uploader :image, ImageUploader

end

和event.rb

class Event < ActiveRecord::Base

attr_accessible :title, :date, :about

validates :title, :about, :date, :presence => :true

validates :title, :uniqueness => :true

has_many :images

extend FriendlyId

friendly_id :title, use: [:slugged, :history]

end

在迁移中运行此文件即可。 列未添加。

def up
   change_table :images do |t|
      t.references :event
   end
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM