ruby-on-railsrubymvppresenter

Using index with Presenter causes NoMethodError


I tried to learn about presenter and did the following classes

User model with id, first_name, last_name, email

user_controller with the following methods

  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

When I go to localhost:3000/users/id it works well but when I go to localhost:3000/users I get the following error

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)

It seems like it doesn't passed as single users


Solution

  • Rather than this:

    render_user_json @users
    

    Either make another method render_users_json (note the plural users) or just call the render from the index method directly like this:

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