Hi, I am writing an application with nested resources. I have a resource called Facilities which has_many Reservations. map.resources :facilities do |facilities| facilities.resources :reservations end I have thus urls like /facilities/1/reservations /facilities/1/reservations/4 I have the possibility to show a list of all reservations - the index action, and a particular reservation - the show action. How do I list all reservations between 10. April 10:00 and 12. April 20:00 ? How do I list ranges in RESTful resources? Cheers, evgeni --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Add a select (you can probably think of a better name) action to the
reservations_controller and pass the start date and end date to it
through the URL?
For example:
In routes.rb:
map.resources :facilities do |facilities|
facilities.resources :reservations, :collection => { :select => :get }
end
In your views:
<%= link_to select_facility_resources(@facility, :start_date =>
@start_date, :end_date => @end_date) -%>
Might want to change the format of the dates you pass in the URL.
In reservations_controller:
# GET /facilities/1/reservations/select
# GET /facilities/1/reservations/select.xml
def select
...
# Find the reservations using params[:start_date] and
params[:end_date]
# Also might want to perform some checking on these dates (encapsulate
in Reservation model?)
@reservations = @facility.reservations.find..(........)
respond_to do |format|
format.html { render :action => :index } # Or use select.html.erb
format.xml { render :xml => @reservations }
end
end
An other option is to simply re-use the index action (rather than a
separate action) for this, and check in there whether a date range is
passed in. If so, find the reservations based on that range, otherwise
default to finding all reservations. Not sure which option is more
elegant.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Hi Gerjan, thank you for your proposal. Sounds really streightforward. Is this approach along the RESTful lines? I have read the following discussion: http://groups.google.ca/group/rubyonrails-talk/browse_thread/thread/e1759be395695179/2cd53b5742b7bec2 I have been thinking about my problem for a couple of days now and I still think there should be a simpler solution. Maybe ranges should be treated as another resource. But then they share the same set of data with the index action. Sounds like a "2 Controller-one Model-thing" DHH was talking on the Eurocon about. Hmm ... Cheers, evgeni --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---