Ruby on rails UsersControllernew中的NoMethodError(未定义的“userprofiles”方法)
我不知道我为什么会犯这种错误。。我已经在以前的项目简单实践项目中制作了一些嵌套资源,但我不知道为什么它不起作用 错误:Ruby on rails UsersControllernew中的NoMethodError(未定义的“userprofiles”方法),ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我不知道我为什么会犯这种错误。。我已经在以前的项目简单实践项目中制作了一些嵌套资源,但我不知道为什么它不起作用 错误: NoMethodError in UsersController#new (undefined method `userprofiles' for #<User:0x000000059c50e0>) 用户\u controller.rb 用户模型 class User < ActiveRecord::Base has_many :user_prof
NoMethodError in UsersController#new (undefined method `userprofiles' for #<User:0x000000059c50e0>)
用户\u controller.rb
用户模型
class User < ActiveRecord::Base
has_many :user_profiles
end
应该是这样的谢谢你的回复,先生,它是有效的,但是如果一对一的关系出现了呢?在Usermodel中有一个:user\profile,并且在UserProfilemodel中属于用户。。我试过了,但没用。然后你可以使用@user.build\u user\u profileSir,非常感谢你的回复,它真的很有效。我第一次遇到构建词,但我会搜索它。对不起,我是rails新手。你的回答对我帮助很大。我希望每次我在rails遇到麻烦时都能打电话给你。谢谢当然请阅读rails指南。顺便问一下,先生,我能告诉你在哪里?能给我你的gmail帐号吗?
class UsersController < ApplicationController
before_action :authenticate_user!
before_action :set_userprofile, only: :new
def index
if current_user
@user = User.find_by_email(current_user.email)
redirect_to :action => "new"
end
end
def new
@user = User.find(current_user.id)
@user_profile = @user.userprofiles.new
end
private
def set_userprofile
@user = User.find(current_user.id)
end
end
class User < ActiveRecord::Base
has_many :user_profiles
end
class UserProfile < ActiveRecord::Base
belongs_to :user
end
def new
...
@user_profile = @user.user_profiles.new
end