【问题标题】:rails 4 - undefined method `group_by' for nil:NilClassrails 4 - nil:NilClass 的未定义方法“group_by”
【发布时间】:2014-12-24 16:02:52
【问题描述】:

我收到一个我似乎不明白的错误。任何帮助将非常感激。

  1. 我有一个用户列表,但只想显示使用内置 ruby​​ 函数“group_by”创建事件的用户。但我收到错误“nil:NilClass 的未定义方法 `group_by'”

控制台:

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

用户/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>

【问题讨论】:

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


    【解决方案1】:

    您没有在UsersController#index 中定义@events

    【讨论】:

    • 谢谢,我更新了我对@events 的看法,但仍然遇到同样的错误
    • 哦,你甚至没有在UsersController#index 中定义@events
    • 感谢@mmicheal - 你是对的......没有在 UsersController#index 方法中定义它 -
    【解决方案2】:

    您的操作并未定义您尝试在视图中使用的实例变量。

    UsersController 内的index 操作中,您需要定义@events 变量。

    类似于EventsController 的索引操作中的内容

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

    【讨论】:

    • 谢谢@humza - 我没有在 UserController.rb 文件的 index 方法中定义它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-28
    • 2017-06-06
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多