0
votes

I am having a problem with structuring rails application. Let's say that I have a Style model and I want to have listed 5 featured ones on my page root (/), and browse option where I can set listing from the oldest to the newest and vice versa.

http://example.com/ - five featured http://example.com/styles/newest - all newest http://example.com/styles/oldest - all oldest

How do I code the controller and routes? I've tried separate controllers for five featured and for all with if statement ( something like if params[:order] == 'oldest' then @styles = Style.oldest), but this doesn't seem to be either working or logical.

1
please add your existing code, controllers, models, routes. and an error if that is what led to the question. I'll think about it in the meantime.OpenCoderX

1 Answers

1
votes

Add two custom routes. Routes.rb

resources :styles do
  collection do
    get :newest
    get :oldest
  end
end

root :to => 'styles#index'

StylesController.rb

def index
   @styles = Style.last(5)
end

def newest
   @styles = Style.order("created_at desc")
  ...
end

def oldest
  ...
end

Suggestion: Keep it simple.