UsersController中的ArgumentError #index错误的参数个数(2个为1)

时间:2015-06-26 02:16:59

标签: ruby-on-rails-4 controller helper railstutorial.org gravatar

我正在关注Michael Hartl的Rails教程,我在第9章第3节。

我应该得到:

enter image description here

但相反,我得到了:

enter image description here

或者,用明文:

ArgumentError in UsersController#index

wrong number of arguments (2 for 1)

Extracted source (around line #4):

    # Returns the Gravatar 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")
Rails.root: /Users/TXC/code/microblog

Application Trace | Framework Trace | Full Trace
app/helpers/users_helper.rb:4:in `gravatar_for'
app/views/users/index.html.erb:7:in `block in _app_views_users_index_html_erb___2825860878015368470_70324743747280'
app/views/users/index.html.erb:5:in `_app_views_users_index_html_erb___2825860878015368470_70324743747280'
Request

Parameters:

None
Toggle session dump

以下是我的用户帮助程序的内容:

module UsersHelper

  # Returns the Gravatar 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

这是用户控制器:

class UsersController < ApplicationController
  before_action :logged_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
      log_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

    # Confirms a logged-in user.
    def logged_in_user
      unless logged_in?
        store_location
        flash[:danger] = "Please log in."
        redirect_to login_url
      end
    end

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

理解出现问题的任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

按照此主题中的建议修复了问题:Wrong number of arguments?

以下是User Helper中的代码:

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

答案 1 :(得分:0)

根据教程,gravatar_for方法定义为

app/helpers/users_helpers.rb
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

请注意,它只接受一个参数:用户。在第7章的后面,在练习之后,教程描述了如何添加大小参数:

   # Returns the Gravatar (http://gravatar.com/) for the given user.
   app/helpers/users_helpers.rb
 def gravatar_for(user, options = { size: 50 })
   gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
   size = options[:size]
   gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?  s=#{size}"
   image_tag(gravatar_url, alt: user.name, class: "gravatar")
 end

根据您的错误消息判断,您尚未更新方法以使用可选尺寸参数