755
755

Reputation: 3081

Simple Rails routes questions

I have

resources :calendars do

   resources :blocked_times

end

What is the helper method that will generate /calendar/:calendar_id/blocked_times ?

I tried calendar_blocked_time_path(@calendar.id) but that gave me:

/calendar/:calendar_id/blocked_times/:blocked_time_id

And the :blocked_time_id is defaulted to 1.

Upvotes: 1

Views: 54

Answers (2)

zeantsoi
zeantsoi

Reputation: 26193

Since you have a nested route, you'll need in more than one argument to calendar_blocked_time_path in order to route to the correct BlockTime:

calendar_block_time_path(@calendar, @blocked_time)

If you want to get the index action for the BlockTimesController, use the following path, passing only the Calendar id:

calendar_block_times_path(@calendar)

Notice that, since you're trying to access a plural resource (block times, in this case), you'll want to invoke the pluralize form of the resource – that is, calendar_block_times.

Remember that you can always run rake routes from the command line to get a full host of the routes and their corresponding paths. Here's the output in your case:

       calendar_blocked_times GET    /calendars/:calendar_id/blocked_times(.:format)          blocked_times#index
                              POST   /calendars/:calendar_id/blocked_times(.:format)          blocked_times#create
    new_calendar_blocked_time GET    /calendars/:calendar_id/blocked_times/new(.:format)      blocked_times#new
   edit_calendar_blocked_time GET    /calendars/:calendar_id/blocked_times/:id/edit(.:format) blocked_times#edit
        calendar_blocked_time GET    /calendars/:calendar_id/blocked_times/:id(.:format)      blocked_times#show
                              PUT    /calendars/:calendar_id/blocked_times/:id(.:format)      blocked_times#update
                              DELETE /calendars/:calendar_id/blocked_times/:id(.:format)      blocked_times#destroy
                    calendars GET    /calendars(.:format)                                     calendars#index
                              POST   /calendars(.:format)                                     calendars#create
                 new_calendar GET    /calendars/new(.:format)                                 calendars#new
                edit_calendar GET    /calendars/:id/edit(.:format)                            calendars#edit
                     calendar GET    /calendars/:id(.:format)                                 calendars#show
                              PUT    /calendars/:id(.:format)                                 calendars#update
                              DELETE /calendars/:id(.:format)                                 calendars#destroy

As you'll notice, the correct path to a calendar block time is /calendars/:calendar_id/blocked_times/:id. The first argument passed to calendar_block_time_path is the :calendar_id, and the second argument passed is :id – that is, the id of the BlockTime.

Upvotes: 3

Brendon Muir
Brendon Muir

Reputation: 4612

With those routes in place run rake routes and you'll get an output of the names of the routes.

Though I think in this case you're looking for:

calendar_blocked_times_path(@calendar)

I haven't tested this, but basically for the index action you want a plural for the second resource.

Upvotes: 2

Related Questions