因此,我正在开发个人项目以了解有关轨道嵌套的内容,并且我决定使用Devise。我创建了一个配置文件模型和控制器来处理显示用户稍后输入的其他信息。并利用after_create :create_profile
来处理繁重的工作。我终于在模型和控制器中设置了一切,以便信息正确传递,但是当涉及到实际显示用户的配置文件时,我的困惑就出现了。在视图混淆中显示配置文件
这是我的个人资料控制器
class ProfileController < ApplicationController
before_action :find_profile
before_action :find_user
before_action :authenticate_user!
def new
@profile = Profile.new
end
def create
@profile = current_user.create_profile(profile_params)
end
def show
@profile = @user.profile
end
def update
@profile.update_attributes(profile_params)
if @profile.save
redirect_to profile_path(current_user)
end
end
private
def find_user
@user = User.find(params[:id])
end
def find_profile
@profile = Profile.find(params[:id])
end
def profile_params
params.require(:profile).permit(:tagline, :age, :website)
end
end
在轨控制台,我可以操纵数据很容易,但在实际使用中(视图)我觉得这是不是这样的。
例如,如果我想显示第二个用户的配置文件,在rails控制台中,我可以简单地做到这一点。
@u = User.find(params[:id])
找到用户,然后我可以做@u.profile
来抓取他的关联配置文件。
因为我将会在很大程度上寻找用户,所以创建一个方法并使用before_action
是有意义的。所以我想我可以在用户模型的标记行上查看<%= @user.profile.tagline %> to grab the user's tagline from their tagline. But that's not working out too well. I also tried
@ profile.user.tagline , which displays nothing (no error is thrown because I added
attr_accessor`)。
任何人有任何建议吗?提示我在做什么错了?
小心张贴您的日志?什么不太好意味着什么? – adamliesko
对不起,我的意思是它根本不显示。我对rails比较新,所以我会特别抓取哪些日志?启动rails服务器后显示的信息? – BronzeTax