【发布时间】:2011-04-19 21:08:01
【问题描述】:
嗨(这里是 Rails 大新手),我有以下型号:
class Shop < ActiveRecord::Base
belongs_to :user
validates_uniqueness_of :title, :user_id, :message => "is already being used"
end
和
class User < ActiveRecord::Base
has_one :shop, :dependent => :destroy
end
当我要创建一个新商店时,我收到以下错误:
private method `create' called for nil:NilClass
这是我的控制器:
@user = current_user
@shop = @user.shop.create(params[:shop])
我通过到处阅读指南和教程尝试了不同的变体,但我比以前更加困惑,无法让它发挥作用。任何帮助将不胜感激。
【问题讨论】:
-
编辑问题标题以反映问题。与Using build with a has_one association in rails 重复
-
你也可以使用
@user.build_shop(params)
标签: ruby-on-rails