Domanda

Chapter: 9.2 pages 405
Problem: None of the users names or avatars are being displayed.

enter image description here

users/index

<% 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>

UsersController

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

Please let me know if I am missing any code you need to see, and if there is any misunderstanding in my question. Thank you for your help and time.

È stato utile?

Soluzione

You're not actually printing out the content in your views.

<% will evaluate the content but not print it; <%= will evaluate it and print it.

So you need to modify these two lines:

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

to add the missing =.

Altri suggerimenti

Use %= to display in HTML:

<li>
  <%= gravatar_for user, size: 52 %>
  <%= link_to user.name, user %>
</li>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top