我的Rails应用程序出了问题,我将其部署到Heroku。 当我在我的俱乐部资源上发帖时,它说:
Parameters: {"utf8"=>"✓","authenticity_token"=>"MqScpac6l9MMHdPBehqfq0mTwLCCZq2dlZLVY6wt+ow=",
"club"=> {"name"=>"Grün Weiß Club im PSV Kiel"}, "commit"=>"absenden"}
2012-12-31T03:10:49+00:00 app[web.1]: NoMethodError (undefined method `user_id=' for # <Club:0x000000061269b0>):
2012-12-31T03:10:49+00:00 app[web.1]:
2012-12-31T03:10:49+00:00 app[web.1]:
2012-12-31T03:10:49+00:00 app[web.1]: app/controllers/clubs_controller.rb:47:in `create'
我的模型看起来像这样:
class Club < ActiveRecord::Base
default_scope :order => 'name ASC'
attr_accessible :name, :user_id
belongs_to :user
has_many :memberships
has_many :users, :through => :memberships, :order => 'name ASC'
validates :name, presence: true, :uniqueness => true
end
并且控制器创建这样的动作:
def create
@club = Club.new(params[:club])
@club.user_id = current_user.id
respond_to do |format|
if @club.save
format.html { redirect_to @club, notice: 'Club was successfully created.' }
format.json { render json: @club, status: :created, location: @club }
else
format.html { render action: "new" }
format.json { render json: @club.errors, status: :unprocessable_entity }
end
end
end
验证由Devise gem完成。另外让我感到困惑的是,如果我在heroku上运行rails控制台,它会说:
irb(main):001:0> Club
=> Club(id: integer, name: string, created_at: datetime, updated_at: datetime, user_id: integer)
所以即使数据库应该是正确的。 谢谢你的帮助!
答案 0 :(得分:0)
在控制器中试一试,看看它是否有效:
@club = current_user.clubs.build params[:club]
而不是
@club = Club.new(params[:club])
@club.user_id = current_user.id
确保User
包含has_many :clubs
,然后您可以摆脱attr_accessible :user_id