错误:未定义的方法`每个'为零:NilClass

时间:2014-03-24 14:16:08

标签: ruby-on-rails ruby ruby-on-rails-4

我收到了这个错误:未定义的方法`每个' for nil:NilClass 在尝试渲染时 choose_album_to_add.html.erb

<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中。 我有这样的路线:

匹配&#39; / addAlbum /:id&#39;,来:&#39;用户#choose_album_to_add&#39;,来自:&#39; get&#39;

当然,我在浏览器上输入/ addAlbum / 45,并且它正确路由。该视图也是肯定加载的,但问题是我在控制器上声明的实例变量无法在我的视图中访问。 = [

我已经搭建了四个资源,他们按照声明实例变量并在视图中使用then的约定来正常工作。我知道有其他方法(比如传递本地人),但我想以这种方式完成工作。

尝试访问

时来自控制台的日志
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'

当我在Console上运行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">]> 

这是我通过scaffold生成的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

任何约会更多?感谢

2 个答案:

答案 0 :(得分:5)

您的方法def choose_album_to_add是私有的 - 将定义移到private语句上方。

由于choose_album_to_add是一个动作,因此必须是视图使用的公共方法。我认为你已经证明当你试图设置@albums = 1并且它没有任何区别时,该方法没有被调用。

答案 1 :(得分: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