2014-03-24 48 views
1

,我發現了這個錯誤:未定義的方法`每個' 近親:NilClass試圖使choose_album_to_add.html.erb時:錯誤:未定義的方法`每個 '近親:NilClass

<table class="table_"> 
    <thead> 
    <tr> 
     <th>Album</th> 
    </tr> 
    </thead> 

    <tbody> 
    <% @albums.each do |album| %> 
     <tr> 
     <td><%= album.name %></td> 
     </tr> 
    <% end %> 
    </tbody> 
</table> 

<br> 

users_controller.rb

def choose_album_to_add 
     @albums = Album.all 
end 

出了什麼問題? 編輯

問:我確定我的視圖文件choose_album_to_add.html.erb位於/ app/views/users中。 我已經像這樣的路線:

匹配 '/ addAlbum /:身份證',爲: '用戶#choose_album_to_add',通過: '得到'

當然,我打字/ addAlbum/45在瀏覽器上,並正確路由。該視圖也被加載,但問題是我的控制器上聲明的實例變量無法在我的視圖中訪問。 = [

我腳手架四個資源,他們工作正常遵循這個慣例聲明實例變量,然後在視圖中使用。我知道還有其他方式(比如傳球當地人),但我想這樣做。

從控制檯日誌tryng訪問

Started GET "/addAlbum/50" for 127.0.0.1 at 2014-03-24 09:56:13 -0400 
Processing by UsersController#choose_album_to_add as HTML 
    Parameters: {"id"=>"50"} 
    Rendered users/choose_album_to_add.html.erb within layouts/application (1.0ms) 
Completed 500 Internal Server Error in 3ms 

ActionView::Template::Error (undefined method `each' for nil:NilClass): 
    14: </thead> 
    15: 
    16: <tbody> 
    17:  <% @albums.each do |album| %> 
    18:  <tr> 
    19:   <td><%= album.name %></td> 
    20:   <td><%= album.created_on %></td> 
    app/views/users/choose_album_to_add.html.erb:17:in `_app_views_users_choose_album_to_add_html_erb___3017203676622264637_69974786999340' 

時,當我在控制檯運行Album.all我得到:

SELECT "albums".* FROM "albums" 
=> #<ActiveRecord::Relation [#<Album id: 1, name: "Album1", created_on: "2014-03-17 23:33:00", finishes_on: "2014-03-24 13:16:00", created_at: "2014-03-17 23:33:14", updated_at: "2014-03-24 13:16:32">, #<Album id: 2, name: "Album2", created_on: "2014-03-17 23:33:00", finishes_on: "2014-03-24 13:16:00", created_at: "2014-03-17 23:33:28", updated_at: "2014-03-24 13:16:42">]> 

這是我的,我通過REST方法插入兩個元由腳手架產生。

users_controller.rb

class UsersController < ApplicationController 
    before_action :set_user, only: [:show, :edit, :update, :destroy] 

    # GET /users 
    # GET /users.json 
    def index 
    @users = User.all 
    end 

    # GET /users/1 
    # GET /users/1.json 
    def show 
    end 

    # GET /users/new 
    def new 
    @user = User.new 
    end 

    # GET /users/1/edit 
    def edit 
    end 

    # POST /users 
    # POST /users.json 
    def create 
    @user = User.new(user_params) 
    @user.photographer_id = session[:current_photographer_id] 

    respond_to do |format| 
     if @user.save 
     format.html { redirect_to @user, notice: 'Novo usuário cadastrado com sucesso. Um email foi encaminhado a ele contendo as informações de acesso.' } 
     format.json { render action: 'show', status: :created, location: @user } 
     else 
     format.html { render action: 'new' } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /users/1 
    # PATCH/PUT /users/1.json 
    def update 
    respond_to do |format| 
     if @user.update(user_params) 
     format.html { redirect_to @user, notice: 'Dados atualizados com sucesso' } 
     format.json { head :no_content } 
     else 
     format.html { render action: 'edit' } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /users/1 
    # DELETE /users/1.json 
    def destroy 
    @user.destroy 
    respond_to do |format| 
     format.html { redirect_to users_url } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_user 
     @user = User.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def user_params 
     params.require(:user).permit(:name, :email, :password, :endereco, :telefone, :cpf) 
    end 

    def choose_album_to_add 
     @albums = Album.all 
    end 

    end 
end 

任何約會嗎?謝謝

+1

當你執行'Album.all'時,rails控制檯輸出什麼? – itsnikolay

+1

你能告訴我們你的路線嗎? – Oxynum

+0

@vee不能解釋爲什麼'@ albums'是零。它應該是空的 – Oxynum

回答

5

你的方法def choose_album_to_add是私有的 - 移動private上述定義聲明。

由於choose_album_to_add是一個動作必須是視圖使用的公共方法。我認爲您已經證明,當您嘗試設置@albums = 1時,該方法未被調用,並且沒有任何區別。

+0

這就是訣竅!真的非常感謝@Graeme和你們所有人 – Pavarine

+0

很高興我們找到了它 - 這一直令我煩惱(你也是我想象的)整天! – 2014-03-24 20:49:14

+0

我得到這個錯誤DESPITE沒有在我的控制器在Rails 5.0中定義的私有方法。這真的很糟糕(加重)。 Rails的行爲與其自己的文檔直接相沖突。 – Peter

0

加載視圖時真的調用choose_album_to_add嗎? (所以,@album var)正如前面的評論所說,如果問題是一個空表,它應該返回一個空的數組/ active_relation。

另一種方法有收集:

class UsersController < ApplicationController 
    helper_method :albums 

    private 
    def albums 
     @albums ||= Album.all 
    end 
end 

您認爲這些只是調用album.each代替@ album.each

相關問題