将索引与 Presenter 一起使用会导致 NoMethodError

Using index with Presenter causes NoMethodError

我尝试了解 Presenter 并做了以下 classes

ID 为 first_name、last_name、电子邮件

的用户模型

user_controller用以下方法

  def index
    @users = User.all
    render_user_json @users
  end

  def show
    @user = User.find_by_id(params[:id])
    if @user.nil?
      render json: { message: 'User can not be found' }, status: 404
      return
    end

    render_user_json @user
  end

  def render_user_json(user)
    render json: ::Presenters::User::UserPresenter.new(user).generate
  end

UserPresenter class

module Presenters
  module User
    class UserPresenter
      attr_reader :user

      def initialize(user)
        @user = user
      end

      def generate
        {
          id: @user.id,
          first_name: @user.first_name,
          last_name: @user.last_name,
          email: @user.email
        }
      end
    end
  end
end

当我转到 localhost:3000/users/id 时效果很好 但是当我转到 localhost:3000/users 时,出现以下错误

NoMethodError (undefined method `id' for #<User::ActiveRecord_Relation:0x00007f9952ac4930>
Did you mean?  ids):
  lib/presenters/user/user_presenter.rb:14:in `generate'
  app/controllers/users_controller.rb:49:in `render_user_json'
  app/controllers/users_controller.rb:9:in `index'


  Rendered /Users/noammansur/.rvm/gems/ruby-2.3.8/gems/actionpack-4.2.11/lib/action_dispatch/middleware/templates/rescues/_source.erb (4.0ms)
  Rendered /Users/noammansur/.rvm/gems/ruby-2.3.8/gems/actionpack-4.2.11/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.0ms)
  Rendered /Users/noammansur/.rvm/gems/ruby-2.3.8/gems/actionpack-4.2.11/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
  Rendered /Users/noammansur/.rvm/gems/ruby-2.3.8/gems/actionpack-4.2.11/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (108.3ms)
  Rendered /Users/noammansur/.rvm/gems/ruby-2.3.8/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb (0.4ms)
  Rendered /Users/noammansur/.rvm/gems/ruby-2.3.8/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.3ms)
  Rendered /Users/noammansur/.rvm/gems/ruby-2.3.8/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.3ms)
  Rendered /Users/noammansur/.rvm/gems/ruby-2.3.8/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.4ms)
  Rendered /Users/noammansur/.rvm/gems/ruby-2.3.8/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript (91.6ms)
  Rendered /Users/noammansur/.rvm/gems/ruby-2.3.8/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript (0.7ms)
  Rendered /Users/noammansur/.rvm/gems/ruby-2.3.8/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.8ms)
  Rendered /Users/noammansur/.rvm/gems/ruby-2.3.8/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb (210.6ms)

好像没有通过单机用户

而不是这个:

render_user_json @users

创建另一个方法 render_users_json(注意复数 users)或者直接从索引方法调用渲染,如下所示:

  # inside index method
  render json: users.map do |user|
    ::Presenters::User::UserPresenter.new(user).generate
  end