我有一个简单的应用程序,用户模型& Instructor_Profile模型。这两种模式之间的关联是一对一的。我无法让我的视图show.html.erb呈现。我只是想显示从Instructor_Profile模型的单一属性,我得到这个错误:Rails - 使用一对一关联的模型显示Action&View?
NoMethodError在Instructor_profiles#显示零 未定义的方法`名”:NilClass
任何帮助将不胜感激!
Models:
class User
has_one :instructor_profile
class InstructorProfile
belongs_to :user
UsersController:
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
UserMailer.welcome_email(@user).deliver
render 'static_pages/congratulations'
else
render 'new'
end
end
InstructorProfilesController:
def new
@instructor_profile = current_user.build_instructor_profile
end
def create
@instructor_profile = current_user.build_instructor_profile(params[:instructor_profile])
if @instructor_profile.save
flash[:success] = "Profile created!"
redirect_to root_path
else
....
end
end
def show
@user = User.find(params[:id])
@instructor_profile = @user.instructor_profile
end
Views/instructor_profiles/show.html.erb:
<p>Display Name: <%= @user.instructor_profile.name %></p>
您是否尝试过用@ instructor_profile.name? – 2013-04-24 05:31:20
嗨Soni。是的,我也尝试过。同样的结果。 – mattq 2013-04-25 00:52:54