我正在关注Micheal Hartl Ruby on Rails教程。在第12章中,我收到了这个错误。
我正在尝试显示以下内容和关注者,但我无法做到。
以下是代码:
show.html.erb
<% 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>
<span><b>Microposts:</b> <%= @user.microposts.count %></span>
</section>
<section class="stats">
<%= render 'shared/stats' %>
<% if @users.any? %>
<div class="user_avatars">
<% @users.each do |user| %>
<%= 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>
relationship_controller.rb
class RelationshipsController < ApplicationController
before_action :logged_in_user
def create
def create
@user = User.find(params[:followed_id])
current_user.follow(user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
def destroy
@user = Relationship.find(params[:id]).followed
current_user.unfollow(user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
end
user_controller.rb
class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :destroy,
:following, :followers]
before_action:correct_user, only: [:edit, :update]
before_action:admin_user, only: :destroy
def index
@users= User.paginate(page: params[:page])
end
def show
@user= User.find(params[:id])
@microposts = @user.microposts.paginate(page: params[:page], :per_page => 5)
end
def new
@user= User.new
end
def create
@user= User.new(user_params)
if @user.save
@user.send_activation_email
flash[:info] = "Please check your email to activate your account."
redirect_to root_url
else
render 'new'
end
end
def edit
@user= User.find(params[:id])
end
def update
@user= User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "User deleted"
redirect_to users_url
end
def following
@title = "Following"
@user = User.find(params[:id])
@users = @user.following.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
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
def correct_user
@user= User.find(params[:id])
redirect_to(root_url) unless @user== current_user
end
def admin_user
redirect_to(root_url) unless current_user.admin?
end
end
答案 0 :(得分:2)
您的问题是show
操作实际上是使用@user
而不是@users
实例变量。
您的统计信息部分不起作用,因为它预期User
的集合看起来像index
行动一样。
由于followers
的操作设置了@user
和@users
个实例变量,您必须访问该操作,而show.html.erb
模板上的代码最多可能必须转移到show_follow.html.erb
模板。
<强>更新强>
您需要从show.html.erb
模板中删除此部分
<section class="stats">
<%= render 'shared/stats' %>
<% if @users.any? %>
<div class="user_avatars">
<% @users.each do |user| %>
<%= 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>
答案 1 :(得分:1)