ARTLoe
ARTLoe

Reputation: 1799

rails 4 - undefined method `group_by' for nil:NilClass

i am getting an error i do not seem to understand. Any help would be much appreciated.

  1. I have a list of users but wanted to display only users that have created events using the inbuilt ruby function 'group_by'. but i get the error "undefined method `group_by' for nil:NilClass"

The console:

Started GET "/users" for 127.0.0.1 at 2014-12-24 15:54:01 +0000
Processing by UsersController#index as HTML
  User Load (0.4ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 40  ORDER BY "users"."id" ASC LIMIT 1
  Rendered users/index.html.erb within layouts/application (1.1ms)
Completed 500 Internal Server Error in 5ms

ActionView::Template::Error (undefined method `group_by' for nil:NilClass):
    10:   </tr>
    11: 
    12: 
    13: <% @events.group_by(&:user).each do |user, events| %>
    14:   <%= link_to(image_tag("profile_image_gravatar.png"), user) %>
    15:   <%= user.full_name %>
    16:   <%= link_to 'Show', user %>
  app/views/users/index.html.erb:13:in `_app_views_users_index_html_erb__3564202377022035077_70335954377780'


  Rendered /Users/ARTLoe/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.3ms)
  Rendered /Users/ARTLoe/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
  Rendered /Users/ARTLoe/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (9.5ms)

events_controller.rb

class EventsController < ApplicationController
  before_action :set_event, only: [:show, :edit, :update, :destroy]
  before_filter :authenticate_user!

  def index
    @events = Event.order(:date)
    # @events = current_user.events | displays only events by current user
  end

  def show
    @commentable = @event
    @comments = @commentable.comments
    @comment = Comment.new
  end

  def new
    @event = Event.new
  end

  def edit
  end

  def create
    @event = Event.new(event_params)
    @event.user = current_user

    respond_to do |format|
      if @event.save
        format.html { redirect_to @event, notice: 'Event was successfully created.' }
        format.json { render :show, status: :created, location: @event }
      else
        format.html { render :new }
        format.json { render json: @event.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @event.update(event_params)
        format.html { redirect_to @event, notice: 'Event was successfully updated.' }
        format.json { render :show, status: :ok, location: @event }
      else
        format.html { render :edit }
        format.json { render json: @event.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @event.destroy
    respond_to do |format|
      format.html { redirect_to events_url, notice: 'Event was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def event_params
      params.require(:event).permit(:name, :description, :date, :time, :city, :price, :user_id)
    end
end

users_controller.rb

class UsersController < ApplicationController
  before_action :set_user, only: [:show]
  before_filter :authenticate_user!

  def index
    @users = User.all
  end

  def show
    @user = User.find(params[:id])
    @user_events = @user.events.order(:date)
    @events = Event.all
  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(:email, :password, :password_confirmation, :firstname, :lastname, :dob, :gender, :description, :role)
    end
end

users/index.html.erb

<% provide(:title, 'Users') %> 
<h1>Listed Event Organizers</h1>

<% @events.group_by(&:user).each do |user, events| %>
  <%= link_to(image_tag("profile_image_gravatar.png"), user) %>
  <%= user.full_name %>
  <%= link_to 'Show', user %>
<% end %>

<h6><%= link_to 'Create Event', new_event_path %></h6>

Upvotes: 1

Views: 1926

Answers (2)

SHS
SHS

Reputation: 7744

Your action isn't defining the instance variable that you're trying to use in the view.

In your index action within the UsersController, you need to define the @events variable.

Something like in the index action of your EventsController

class class UsersController < ApplicationController
  def index
    @users = User.all
    @events = Event.includes(:user)
  end
end

Upvotes: 1

vich
vich

Reputation: 11896

You're not defining @events in UsersController#index.

Upvotes: 2

Related Questions