Appointment History template page

Ok so I got an Appointments page (index.html.erb)

How would I go about adding an Appointment History page?

1) adding a history.html.erb and editing my routes?

or

2) use some kind of get parameter (e.g. /appointments/history) and adding it to the appointments controller (eg def history )?

Ok so I got an Appointments page (index.html.erb)

How would I go about adding an Appointment History page?

1) adding a history.html.erb and editing my routes?

I presume you mean adding a history controller

or

2) use some kind of get parameter (e.g. /appointments/history) and adding it to the appointments controller (eg def history )?

Either is ok, do it whichever way you think is most appropriate. One thing to consider is what the views will be like. If the history is very like your index view then personally I would use a parameter on the appointments controller that just causes the appropriate records to be selected. If you think of the history view as being completely different then do it with it's own controller.

Either way make sure you write your tests first so that if you decide later that you prefer the other way then you can be confident that you have re-factored the code correctly when all the tests pass.

Colin

If the history is very like your index view then personally I would use a parameter on the appointments controller that just causes the appropriate records to be selected. If you think of the history view as being completely different then do it with it's own controller.

This is what I tried on the APPOINTMENTS_CONTROLLER   def index(display='index')     unless display=='history'       #past appointments       @appointments = Appointment.all(:order => 'start', :conditions => [ "start < ?", Date.today ] )    else       @appointments = Appointment.all(:order => 'start', :conditions => [ "start >= ?", Date.today ] )       appointments = Appointment.all(:order => 'start', :conditions => [ "start >= ?", Date.today ] )

      appointments.group_by do |appointment|         appointment.start.strftime("%Y%m%d")       end     end

Then I go to this URL: http://localhost:3000/appointments/history

I get error... ActiveRecord::RecordNotFound in AppointmentsController#show Couldn't find Appointment with ID=history

But it's pointing to the show action.

It's kinda obvious by the error message what the problem is, thing is, I don't know how to achieve what I want.

What you wanted to do could be achieved by routing:

http://guides.rubyonrails.org/routing.html#adding-more-restful-actions

HTH

Rails Routing from the Outside In — Ruby on Rails Guides

Tried it, same error. On the controller I even tried...

  resources :appointments do     get 'history' => :history   end   get "appointments/history"

Got it, I think when I restarted the server, it didn't see the changes or something.

ROUTES   resources :appointments do     collection do       get 'history' => 'appointments#index'     end   end

CONTROLLER   def index      if request.request_uri=='/appointments/history'       #past appointments       @appointments = Appointment.all(:order => 'start', :conditions => [ "start < ?", Date.today ] )     else       @appointments = Appointment.all(:order => 'start', :conditions => [ "start >= ?", Date.today ] )     end

I just probably need to use a helper for the link in the if statement

IMPROVED.