【发布时间】:2011-07-19 07:58:08
【问题描述】:
我正在尝试为 Profiles 创建一个关联的表单,但由于某种原因,当我点击提交按钮时,我得到一个 NoMethodError,这对我来说没有意义,因为我的代码与我的教程完全相同我正在关注...除非教程已过时..def create@user = User.find(params[:user_id])@profile = @user.profiles.create(params[:profile])redirect_to user_path(@user)end
有人知道我为什么会收到 noname 错误吗?
属于控制器的表格如下:
https://github.com/imjp/SuperModel/blob/master/app/views/users/show.html.erb
编辑 1: 已修复!以下代码未在 http://localhost:3000/users/2(即 profile#show)处显示配置文件数据:<%= @user.profile.first_name %>
这是我目前的个人资料#showdef show@user = User.find(params[:user_id])@profile = @user.profile.find(params[:id])end
编辑 2: 我已经更新我的 github 仓库https://github.com/imjp/SuperModel
【问题讨论】:
-
您可以尝试使用create_profile! (使用 ! bang 版本)检查是否引发任何异常。可能是配置文件模型上的验证错误或 attr_accessible/protected 集。
-
不,它不起作用。我已经在上面粘贴了我的显示操作。知道什么可能导致该操作引发此错误吗?
undefined method 'first_name' for nil:NilClass -
无需额外查找配置文件,只需 @profile = @user.profile
-
def show @user = User.find(params[:user_id]) @profile = @user.profile end虽然不起作用..我仍然得到同样的错误 -
那么配置文件没有正确创建,这是另一回事。当您创建配置文件时,请尝试使用我上面写的 bang 版本,看看是否有错误以及记录是否在数据库中正确创建,检查日志。
标签: ruby-on-rails