Ruby on rails Rails:NoMethodError在创建时与Desive模型有一个关联

Ruby on rails Rails:NoMethodError在创建时与Desive模型有一个关联,ruby-on-rails,ruby,devise,Ruby On Rails,Ruby,Devise,我是Rails的完全初学者,因此我正在尝试构建一个页面,以便在用户登录后添加额外的配置文件数据 我使用Desive进行身份验证,效果很好。我犯了这个错误,我被困在这里了 未定义的方法“profiles” 你能帮忙吗 代码 profiles_controller.rb 其他参考代码: /视图/配置文件/new.html.erb models/user.rb class用户

我是Rails的完全初学者,因此我正在尝试构建一个页面,以便在用户登录后添加额外的配置文件数据

我使用Desive进行身份验证,效果很好。我犯了这个错误,我被困在这里了

未定义的方法“profiles”

你能帮忙吗

代码

profiles_controller.rb

其他参考代码:

/视图/配置文件/new.html.erb

models/user.rb

class用户
models/profile.rb

类配置文件
您试图调用未定义的关系:

  def new
    @profile = current_user.profiles.build
  end

  has_one :profile
您应该拨打:

  def new
    @profile = current_user.build_profile
  end

您正在尝试调用未定义的关系:

  def new
    @profile = current_user.profiles.build
  end

  has_one :profile
您应该拨打:

  def new
    @profile = current_user.build_profile
  end
我刚想出来

由于这种关系是有一个,我们应该使用

def new
   @profile = current_user.build_profile
end
而不是

def new
   @profile = current_user.profiles.build
end
根据文档-

我刚想出来

由于这种关系是有一个,我们应该使用

def new
   @profile = current_user.build_profile
end
而不是

def new
   @profile = current_user.profiles.build
end
根据文档-

1)如果您的用户必须具有多个配置文件。在你的app/models/user.rb
中设置有许多:配置文件

2)在档案控制器中,使用新方法,而不是
@profile=current\u用户。档案使用
@profile=profile.new

3)在您的路线中。rb删除

  get 'profiles/new'

  get 'profiles/create'

  get 'profiles/show'

  get 'profiles/update'
因为您已经使用了
resources:profiles

4)要遵守干燥规则,可以从局部渲染形状。只需在新的.html.erb中添加具有相同内容的views/profiles/_form.html.erb,然后可以删除im new.htm.erb中的所有内容并粘贴
。将来,如果需要,它将帮助您呈现编辑表单

5)在ProfilesController中,可以为所有配置文件添加方法索引

def index
  @profiles = Profile.all
end
1)如果您的用户必须有多个配置文件。在你的app/models/user.rb
中设置有许多:配置文件

2)在档案控制器中,使用新方法,而不是
@profile=current\u用户。档案使用
@profile=profile.new

3)在您的路线中。rb删除

  get 'profiles/new'

  get 'profiles/create'

  get 'profiles/show'

  get 'profiles/update'
因为您已经使用了
resources:profiles

4)要遵守干燥规则,可以从局部渲染形状。只需在新的.html.erb中添加具有相同内容的views/profiles/_form.html.erb,然后可以删除im new.htm.erb中的所有内容并粘贴
。将来,如果需要,它将帮助您呈现编辑表单

5)在ProfilesController中,可以为所有配置文件添加方法索引

def index
  @profiles = Profile.all
end

你能公布全部错误吗?还有,你能发布你的用户模型吗?嘿,安东尼,我刚想出来!这段关系很好。因此,它应该是
@profile=current\u user.build\u profile
而不是
@profile=current\u user.profiles.build
您能发布完整的错误吗?还有,你能发布你的用户模型吗?嘿,安东尼,我刚想出来!这段关系很好。因此,它应该是
@profile=current\u user.build\u profile
,而不是
@profile=current\u user.profiles.build
,谢谢Jorge。但这使得构建方法没有定义。我刚想出来。它在文档中:(谢谢Jorge。但是这给了构建方法没有定义。我刚刚弄明白了。它在文档中:(