I have a messages controller thats declared as a resource in my routes.
I want to be able to have a list view of received messages and a different view of sent messages.
But I dont want to break the rest pattern... What do you guys reccomend?
I have a messages controller thats declared as a resource in my routes.
I want to be able to have a list view of received messages and a different view of sent messages.
But I dont want to break the rest pattern... What do you guys reccomend?
Keep it simple
/messages?view=sent
In your index
method
def index
@messages = case params[:view]
when 'sent'
Messages.sent
when 'received'
Messages.received
else
Messages.all
end
end
See how the inherited_resources
plugin handles Scoping.
in config/routes.rb:
map.resources :messages, :collection => { :sent => :get, :received => :get }
Then in your messages_controller.rb:
def received
@messages = Message.to(current_user)
end
def sent
@messages = Message.from(current_user)
end
In your message.rb define these two named scope methods.
Or as Tony Fontenot pointed out:
def received
@messages = current_user.messages.to
end
def sent
@messages = current_user.messages.from
end