0

我单击了我的 sample_app 的用户选项卡,我得到错误的参数数量错误(2 比 1)。

index.html.erb

说错误在于代码行 <%= gravatar_for user, size: 52 %>

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

users_controller.rb

class UsersController < ApplicationController
  before_action :signed_in_user, only: [:index, :edit, :update]
  before_action :correct_user,   only: [:edit, :update]

 def index
    @users = User.all
  end

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

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      sign_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end
def edit
  end

def update
    if @user.update_attributes(user_params)
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render 'edit'
    end
  end

 private

    def user_params
      params.require(:user).permit(:name, :email, :password,
                                   :password_confirmation)
    end

    # Before filters

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

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

session_helper.rb

module SessionsHelper

 def sign_in(user)
    remember_token = User.new_remember_token
    cookies.permanent[:remember_token] = remember_token
    user.update_attribute(:remember_token, User.encrypt(remember_token))
    self.current_user = user
  end

  def signed_in?
    !current_user.nil?
  end

    def current_user=(user)
    @current_user = user
  end

  def current_user
    remember_token = User.encrypt(cookies[:remember_token])
    @current_user ||= User.find_by(remember_token: remember_token)
  end

  def current_user?(user)
    user == current_user
  end

  def sign_out
    self.current_user = nil
    cookies.delete(:remember_token)
  end

  def redirect_back_or(default)
    redirect_to(session[:return_to] || default)
    session.delete(:return_to)
  end

   def store_location
    session[:return_to] = request.url if request.get?
  end
end

users_helper.rb

module UsersHelper

  # Returns the Gravatar (http://gravatar.com/) for the given user.
  def gravatar_for(user)
    gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
    gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
    image_tag(gravatar_url, alt: user.name, class: "gravatar")
  end
end

我知道我一定错过了一些代码行,或者在某个地方拼错了一些东西。有谁知道可能是什么问题?

4

1 回答 1

4

似乎您正在将选项传递给该gravatar_for方法。将第二个参数添加到您的方法定义中,称为options = {}. 然后使用这些选项来定制您的 gravatar 请求。我认为这就是他在教程中的做法。

于 2013-09-21T05:51:31.567 回答