Reputation: 726
I'm getting the this error: undefined method `each' for nil:NilClass when trying to render 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>
my users_controller.rb
def choose_album_to_add
@albums = Album.all
end
What could be wrong? Edit
As asked, I'm sure that my view file choose_album_to_add.html.erb is in /app/views/users . And I've a route like this:
match '/addAlbum/:id', to: 'users#choose_album_to_add', via: 'get'
Of course, I'm typing /addAlbum/45 on Browser, and it routes properly. The view is also loaded for sure, but the problem is that the instance vars that I declared on my controller can't be accessed on my view . =[
I've scaffolded four resources and they're working fine following this conventions of declaring instance variables and using then in views. I know there are alternative ways ( like passings locals ) but I want to do the job this way.
The log from the console when tryng to access
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'
When I run Album.all on Console I get:
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">]>
which is my two tuples that I inserted via the Rest methods generated by scaffold.
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
Any appointments more? Thanks
Upvotes: 1
Views: 12922
Reputation:
Your method def choose_album_to_add
is private - move the definition above the private
statement.
Since choose_album_to_add
is an action is has to be a public method to be used by the view. I think you already proved the method wasn't being called when you tried to set @albums = 1
and it didn't make any difference.
Upvotes: 5
Reputation: 1
Is really choose_album_to_add being called when loading the view? (and so, the @album var) As some said in previous comments, if the problem was an empty table it should return an empty array/active_relation.
Another way to have that collection is:
class UsersController < ApplicationController
helper_method :albums
private
def albums
@albums ||= Album.all
end
end
Then in your view just call album.each instead of @album.each
Upvotes: 0