3

I have an admin controller that is just used for its index action which renders a view with the most recent updates to certain resources:

class AdminController < ApplicationController

    before_action :authenticate_user!
    layout "admin"

    def index
        @events = Event.last(3)
        @foos   = Foo.last(3)
        @bars   = Bar.last(3)
    end

end

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
  end

  def show
  end

  def new
  end

  def create
  end



end

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
end

Or is there a better approach(perhaps more restful)?

1

In my opinion, you should retrieve the events inside the 'new' action.

The more RESTful alternative would be to have separate methods (one for creating a new event and one for getting a list of previously added events) and then to call out to them from the client in order to ultimately render the page/accept user input.

But, I do not like this approach because it increases the total load time on the client. If you aren't building REST services, but web pages, it's usually better to just forget adhering the back-end strictly to REST; but things like having RESTful URLs are a good thing because they affect user experience.

If you really still need REST, you may consider having separate versions that call the same methods. One action method to maximize page load time, and however many REST methods you need to separate your REST resources.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.