我是 Rails 新手,并试图在我的创建控制器中设置子类的“类型”。我该怎么做?这是我的代码:
Post.rb
class Post < ActiveRecord::Base
attr_accessible :body, :name, :song_id, :user_id, :artist_id, :type
belongs_to :song
belongs_to :user
belongs_to :artist
end
图片.rb
class Picture < Post
end
最后是控制器:
def create
@picture = Post.new(params[:picture])
@picture.type = "Picture"
if @picture.save
redirect_to @artist, :notice => "Successfully posted picture."
else
render :action => 'new'
end
end