Jump to content

The ultimate community for Ruby on Rails developers.


Should I use the new method/action to retrieve records? (Rails)

rails-4 design-patterns restful rest

  • Please log in to reply
1 reply to this topic

#1 mrgrt



  • Members
  • 1 posts

Posted 29 December 2013 - 06:00 PM

I have an admin controller that is just used for its index action which renders a view which displays the most recent updates to certain resources in tables:
    class AdminController < ApplicationController
     before_action :authenticate_user!
     layout "admin"
     def index
     @events = Event.last(3)
            @foos   = Foo.last(3)
            @bars   = Bar.last(3)
Now, I want to create a new event. So I have an Events controller: 

    class EventsController < ApplicationController
      before_action :authenticate_user!, only: [:new,:create]
      layout "admin", only: [:new]
      def index
        @events = Event.all
      def show
      def new
      def create
But I want to display a list of events that have previously been added in the same view as the form for creating a new event. Should I retrieve all
the events in the new action? i.e.

    def new
      @events = Event.all
Or is there a better approach(perhaps more restful)?

#2 james



  • Members
  • 223 posts
  • LocationLeeds, U.K.

Posted 02 January 2014 - 08:19 AM

Firstly there is nothing un RESTfull regarding your approach, you are using the REST actions exactly as they are intended to be used. Your approach is good.


However you now have two controllers both needing authentication.


Can I suggest a better approach to your controller setup that might give you a better understanding of OO design


Create a base public controller and a base admin controller

Instead of directly descending your controllers from application controller take them from either the base public controller or the base admin controller. Move the before action filter into the application controller, provide an empty authenticate_user method in the base public controller and put the proper authenticate_user method into the base admin controller.


This gives you two benefits

1) You will never forget to apply the filter which means you will never get a situation where you are providing admin access to the public.

When you create a new controller and forget to change the class declaration from ApplicationController to either BasePublicController or BaseAdminController you will immediately get a no method error on authenticate_user method as it no longer exists in the application controller.


2) A natural place holder to separate out any other admin specific functionality from public, i.e. different layouts.


Your base controllers are just files in the controller folder that descend from Application controller, they have no methods in them, just the authenticate_user method and as the filter is declared in the application controller you need to do no more.


Hope that makes sense

Programming is just about problem solving!

Also tagged with one or more of these keywords: rails-4, design-patterns, restful, rest

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users