我知道之前已经问过这个问题,但我认为我做的一切都很正确,但仍然无法正常工作。
我有一个简单的用户表,一对多关系表。我只想显示部分中所选用户之后的人数。 它在非局部视图中工作正常,但我一直收到错误:
undefined method `name'
当我将普通视图变为局部视图时,它突出显示错误的焦点为
def gravatar_for(user, size = 30, title = user.name)
这是我使用的辅助方法
部分在这里(称为users/_show_follow
):
<% provide(:title, @title) %>
<div class="row">
<aside class="col-md-4">
<section class="user_info">
<%= gravatar_for user %>
<h1><%= user.name %></h1>
<span><%= link_to "view my profile", user %></span>
</section>
<section class="stats">
<%= render 'shared/stats' %>
<% if users.any? %>
<div class="user_avatars">
<% for user in users %>
<%= link_to gravatar_for(user, size: 30), user %>
<% end %>
</div>
<% end %>
</section>
</aside>
<div class="col-md-8">
<h3><%= @title %></h3>
<% if users.any? %>
<ul class="users follow">
<%= render users %>
</ul>
<%= will_paginate %>
<% end %>
</div>
</div>
部分的原始页面:
<div class="container-fluid">
<div class="row-fluid">
<div class="col-md-8">
<div id="chart"><%= javascript_include_tag('graph') %></div>
</div>
<div class="col-md-2">
<p>
<h2>Node Score</h2>
<div id="FBDiv"></div>
<div id="chart_1"></div>
<h2>Node Profile</h2>
<div id="NodeProfile"></div>
<h2>My inner circle</h2>
<h2>Wants to connect</h2>
<h2>My profile</h2>
<%= current_user.about %>
<section class="stats">
<%= render 'shared/stats' %>
<%= render :partial => "show_follow", :locals => {:user => @user, :users => @users} %>
</section>
</section>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-md-4"></div>
</div>
</div>
控制器
class UsersController < ApplicationController
def index
@users = User.including_relationships
respond_to do |format|
format.html # index.html.erb
format.json { render json: @users }
end
end
def show
@user = User.find_by name: params[:name]
respond_to do |format|
# Here we need to remove the layout because the request
# is done via ajax and the layout is already loaded.
format.json { render json: @user.to_json }
end
end
def following
@title = "Following"
@user = User.find(params[:id])
@users = @user.followed_users.paginate(page: params[:page])
render 'show_follow'
end
def followers
@title = "Followers"
@user = User.find(params[:id])
@users = @user.followers.paginate(page: params[:page])
render 'show_follow'
end
end
the helper
module ApplicationHelper
def page_header(text)
content_for(:page_header) { text.to_s }
end
def gravatar_for(user, title = user.name, size = 30)
image_tag gravatar_image_url(user.email, size: size), title: title, class: 'img-rounded'
end
end
问题是我如何将变量从部分传递给辅助方法?
答案 0 :(得分:0)
正如您所指出的,错误在您的帮助器参数上:def gravatar_for(user, size = 30, title = user.name)
。 user
不是一个对象,它只是参数的名称,因此user.name
无法工作。
尝试设置默认标题(1)或在调用方法(2)时传递标题:
def gravatar_for(user, size = 30, title = 'Default name')
<%= gravatar_for user, 20, user.name %>
。每次重新排序参数时,为避免传递大小:def gravatar_for(user, title = user.name, size = 30)
或使用keyword arguments。