2

我關注的邁克爾·哈特爾的Rails的教程,我在第9章,第3節引發ArgumentError在UsersController#指數錯誤的參數數目(2 1)

我應該得到:

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 

任何幫助,瞭解了什麼錯誤,將不勝感激。

回答

0

繼此線程中的建議解決了這一問題:Wrong number of arguments?

下面是代碼,應該是在用戶助手:

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

通過您的錯誤信息來看,你有沒有更新爲使用可選的尺寸參數的方法

相關問題