0

章节: 9.2 页 405
问题:没有显示用户名或头像。

在此处输入图像描述

用户/索引

<% provide(:title, 'All users') %>
<h1>All users</h1>

<ul class="users">
  <% @users.each do |user| %>
    <li>
      <% gravatar_for user, size: 52 %>
      <% link_to user.name, user %>
    </li>
  <% end %>
</ul>

用户控制器

class UsersController < ApplicationController
  before_filter :signed_in_user, only: [:index, :edit, :update]
  before_filter :correct_user,   only: [:edit, :update]
  def show
    @user = User.find(params[:id])
  end  

  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      # Handle a successful save
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end

  def index
    @users = User.all
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    if @user.user_attributes(params[:users])
      flash[:success] = "Profile updated"
      sign_in @user
      redirect_to @user
    else
      render 'edit'
    end
  end

  private

  def signed_in_user
    redirect_to signin_path, notice: "Please sign in." unless signed_in?
  end

  def correct_user
    @user = User.find(params[:id])
    redirect_to(root_path) unless current_user?(@user)
  end
end

sample_data.rake

namespace :db do
  desc "Fill database with sample data"
  task populate: :environment do
    User.create!(name: "Example User",
                 email: "example@railstutorial.org",
                 password: "foobar",
                 password_confirmation: "foobar")
    99.times do |n|
      name = Faker::Name.name
      email = "example-#{n+1}@railstutorial.org"
      password = "password"
      User.create!(name: name,
                   email: email,
                   password: password,
                   password_confirmation: password)
    end
  end
end

如果我遗漏了您需要查看的任何代码,以及我的问题是否有任何误解,请告诉我。感谢您的帮助和时间。

4

2 回答 2

2

您实际上并没有打印出视图中的内容。

<%将评估内容但不打印;<%=将评估它并打印它。

所以你需要修改这两行:

<% gravatar_for user, size: 52 %>
<% link_to user.name, user %>

添加缺少的=.

于 2013-11-14T01:04:06.943 回答
0

用于%=在 HTML 中显示:

<li>
  <%= gravatar_for user, size: 52 %>
  <%= link_to user.name, user %>
</li>
于 2013-11-14T01:03:49.157 回答