ruby on rails - Error: undefined method `each' for nil:NilClass -


i'm getting error: undefined method `each' nil:nilclass when trying render choose_album_to_add.html.erb :

<table class="table_">   <thead>     <tr>       <th>album</th>     </tr>   </thead>    <tbody>     <% @albums.each |album| %>       <tr>         <td><%= album.name %></td>       </tr>     <% end %>   </tbody> </table>  <br> 

my users_controller.rb

def choose_album_to_add        @albums = album.all end 

what wrong? edit

as asked, i'm sure view file choose_album_to_add.html.erb in /app/views/users . , i've route this:

match '/addalbum/:id', to: 'users#choose_album_to_add', via: 'get'

of course, i'm typing /addalbum/45 on browser, , routes properly. view loaded sure, problem instance vars declared on controller can't accessed on view . =[

i've scaffolded 4 resources , they're working fine following conventions of declaring instance variables , using in views. know there alternative ways ( passings locals ) want job way.

the log console when tryng access

started "/addalbum/50" 127.0.0.1 @ 2014-03-24 09:56:13 -0400 processing userscontroller#choose_album_to_add 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' nil:nilclass):     14:   </thead>     15:      16:   <tbody>     17:     <% @albums.each |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' 

when run album.all on console get:

select "albums".* "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">]>  

which 2 tuples inserted via rest methods generated scaffold.

users_controller.rb

class userscontroller < applicationcontroller   before_action :set_user, only: [:show, :edit, :update, :destroy]    # /users   # /users.json   def index     @users = user.all   end    # /users/1   # /users/1.json   def show   end    # /users/new   def new     @user = user.new   end    # /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 |format|       if @user.save         format.html { redirect_to @user, notice: 'novo usuário cadastrado com sucesso. um email foi encaminhado ele contendo 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 |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 |format|       format.html { redirect_to users_url }       format.json { head :no_content }     end   end    private     # use callbacks share common setup or constraints between actions.     def set_user       @user = user.find(params[:id])     end      # never trust parameters scary internet, allow 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 

any appointments more? thanks

your method def choose_album_to_add private - move definition above private statement.

since choose_album_to_add action has public method used view. think proved method wasn't being called when tried set @albums = 1 , didn't make difference.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -