Ruby nil的未定义方法:NilClass链接到配置文件
我正在尝试从当前用户页面链接到其他用户配置文件页面,当我单击某人的姓名时,我得到以下错误Ruby nil的未定义方法:NilClass链接到配置文件,ruby,ruby-on-rails-3,null,Ruby,Ruby On Rails 3,Null,我正在尝试从当前用户页面链接到其他用户配置文件页面,当我单击某人的姓名时,我得到以下错误:未定义的方法profile\u photo'for nil:NilClass` 因此,我在我的show view中有以下内容: <%- model_class = Account -%> <div class="page-header"> <h1><%=t '.title', :default => model_class.model_name.human
:未定义的方法profile\u photo'for nil:NilClass`
因此,我在我的show view中有以下内容:
<%- model_class = Account -%>
<div class="page-header">
<h1><%=t '.title', :default => model_class.model_name.human.titleize %></h1>
</div>
<dl class="dl-horizontal">
<dd><%= image_tag @account.profile_photo.url %></dd>
<dt><strong><%= model_class.human_attribute_name(:name) %>:</strong></dt>
<dd><%= @account.name %></dd>
<dt><strong><%= model_class.human_attribute_name(:gender) %>:</strong></dt>
<dd><%= @account.gender %></dd>
<dt><strong><%= model_class.human_attribute_name(:age) %>:</strong></dt>
<dd><%= @account.age %></dd>
<dt><strong><%= model_class.human_attribute_name("Date Of Birth") %>:</strong></dt>
<dd><%= (@account.year_Of_Birth.to_s) +"/"+(@account.month_Of_Birth.to_s)+"/"+(@account.day_Of_Birth.to_s) %></dd>
<dt><strong><%= model_class.human_attribute_name(:country) %>:</strong></dt>
<dd><%= @account.country %></dd>
<dt><strong><%= model_class.human_attribute_name(:favorite_Sport) %>:</strong></dt>
<dd><%= @account.favorite_Sport %></dd>
<dt><strong><%= model_class.human_attribute_name(:account_id) %>:</strong></dt>
</dl>
谢谢。问题与链接到标签有关,在这个标签中,第二个参数是显示操作路径。
但您已经传递了对象。
请检查各自控制器和操作的路线
使用命令:rake routes
然后将第二个参数更改为routes_path(account.id)
将路由更改为需要控制器操作的任何路径
有关更多信息,请检查:您可能希望检查并确保在config/routes.rb
中为帐户提供的唯一路由是:
resources :accounts
您的错误消息表明您正在调用帐户上的profile\u photo
,但是帐户
是一个nil
值,而不是一个可以接收方法调用的对象
确保您的accounts\u controller.rb
文件包含以下操作(即方法):
class AccountsController
看起来没有找到@account
-你能检查一下你的控制器上是否设置了@account
?正如我在帖子上说的,我把这个放在控制器上:def show@account=account.find(params[:id])顺便问一下,account是一个设计模型你在哪里设置@accounts
(复数)`?我不是,我是乞丐抱歉,这可能是一个问题,请在您的show action中设置@accounts
:@accounts=Account。所有查看它是否解决了问题
def show
@account = Account.find(params[:id])
end
resources :accounts
class AccountsController < ActionController::Base
def index
@accounts = Account.all
end
def show
@account = Account.find(params[:id])
end
end