0

My app is a workout scheduler.

The client typically does workouts at one of three timeslots in a given day, so I'm providing a quick-add function to schedule a morning, noon or evening workout.

I have the code working, but I don't think I'm doing it the right way.

My route is as follows:

match 'workouts/quick_add/:date/:timeslot' => "workouts#quick_add",
  :as => 'workout_quick_add'

Which I use via something like this:

<%= link_to 'Morning Workout', workout_quick_add_path(:date => day, :timeslot => 'morning') %>

Now, this works, if the request comes in over GET, but that doesn't seem right based on the HTTP Protocol method definitions. It seems like POST or PUT would be right, but if I add :via => :post or :put to the route, the whole thing buggers out with a routing error.

What's correct here, and what's the right way to implement this kind of a function?

DVG
  • 17,392
  • 7
  • 61
  • 88

1 Answers1

2

How about:

resources :workouts do
  collection do
    post :quick_add
  end
end

And pass date and timeslot in params.

DVG
  • 17,392
  • 7
  • 61
  • 88
Tom L
  • 3,389
  • 1
  • 16
  • 14
  • Yeah, I get routing errors when doing this. No route matches :action => quick_add, :controller => workouts, :date => 2012-05-06, :timeslot => morning. This is when using quick_add_workout_path(:date => day, :timeslot => 'morning') – DVG May 07 '12 at 18:37
  • 1
    aha! I made it a collection route and updated the link_to to use `:method => :post` and now everything works the way I wanted it to. Thanks!. If you edit your answer for `collection do` I'll upvote and accept your answer :) – DVG May 07 '12 at 18:51
  • Ah, a collection, of course. You're dealing with creating, not updating. Thanks for the correction and upvote. Cheers. – Tom L May 08 '12 at 15:43