我有一個簡單的應用程序,用戶模型& 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