开发者

Nested routes in Rails with an alias

I have a primary model for my project, Place.rb with a places_controller, and I've currently got it exactly the way I want for the users end of my project. With a nested photos controller, review, etc.

I want now to create a management resource which is mostly just an alias for Places, with its own nested resources, some of them overlapping, some of them new.

I tried creating a new controller for it called Manage, but I'm having a hard time with routes. I'm not quite sure the hangup is, but I figure I'm doing something very wrong. I had little difficulty when I was using Places as controller to a real model and nesting other resources below it.

But for example trying to create a new record for a nested resource doesn't route correctly.

I can get a route path like new_manage_room_path(@place) for a link_to to work fine. But

for creating a New announcement in a form:

form_for manage_room_path(@place) doesn't work correctly given a valid id. I've tried many other combinations supplying the object and :url.

Should I avoid using a separate controller and just create an alias or what is the special routing for this purpose?

map.resources :manage, :collection => { :upcoming => [ :post, :get ], :pending => [ :开发者_如何转开发post, :get ] } do |manage|
  manage.resources :rooms
  manage.resources :room_rates, :as => :rates
  manage.resources :availables
  manage.resources :manage_bookings, :as => :bookings
end

map.resources :places do |place|
  place.resources :bookings
  place.resources :photos, :collection => { :sort => :post }
  place.resources :reviews, :only => [ :index, :show ]
end



manage_rooms GET    /manage/:manage_id/rooms(.:format)               {:controller=>"rooms", :action=>"index"}
                                   POST   /manage/:manage_id/rooms(.:format)               {:controller=>"rooms", :action=>"create"}
                   new_manage_room GET    /manage/:manage_id/rooms/new(.:format)           {:controller=>"rooms", :action=>"new"}
                  edit_manage_room GET    /manage/:manage_id/rooms/:id/edit(.:format)      {:controller=>"rooms", :action=>"edit"}
                       manage_room GET    /manage/:manage_id/rooms/:id(.:format)           {:controller=>"rooms", :action=>"show"}
                                   PUT    /manage/:manage_id/rooms/:id(.:format)           {:controller=>"rooms", :action=>"update"}
                                   DELETE /manage/:manage_id/rooms/:id(.:format)           {:controller=>"rooms", :action=>"destroy"}
                 manage_room_rates GET    /manage/:manage_id/rates(.:format)               {:controller=>"room_rates", :action=>"index"}
                                   POST   /manage/:manage_id/rates(.:format)               {:controller=>"room_rates", :action=>"create"}
              new_manage_room_rate GET    /manage/:manage_id/rates/new(.:format)           {:controller=>"room_rates", :action=>"new"}
             edit_manage_room_rate GET    /manage/:manage_id/rates/:id/edit(.:format)      {:controller=>"room_rates", :action=>"edit"}
                  manage_room_rate GET    /manage/:manage_id/rates/:id(.:format)           {:controller=>"room_rates", :action=>"show"}
                                   PUT    /manage/:manage_id/rates/:id(.:format)           {:controller=>"room_rates", :action=>"update"}
                                   DELETE /manage/:manage_id/rates/:id(.:format)           {:controller=>"room_rates", :action=>"destroy"}
                 manage_availables GET    /manage/:manage_id/availables(.:format)          {:controller=>"availables", :action=>"index"}
                                   POST   /manage/:manage_id/availables(.:format)          {:controller=>"availables", :action=>"create"}
              new_manage_available GET    /manage/:manage_id/availables/new(.:format)      {:controller=>"availables", :action=>"new"}
             edit_manage_available GET    /manage/:manage_id/availables/:id/edit(.:format) {:controller=>"availables", :action=>"edit"}
                  manage_available GET    /manage/:manage_id/availables/:id(.:format)      {:controller=>"availables", :action=>"show"}
                                   PUT    /manage/:manage_id/availables/:id(.:format)      {:controller=>"availables", :action=>"update"}
                                   DELETE /manage/:manage_id/availables/:id(.:format)      {:controller=>"availables", :action=>"destroy"}
            manage_manage_bookings GET    /manage/:manage_id/bookings(.:format)            {:controller=>"manage_bookings", :action=>"index"}
                                   POST   /manage/:manage_id/bookings(.:format)            {:controller=>"manage_bookings", :action=>"create"}
         new_manage_manage_booking GET    /manage/:manage_id/bookings/new(.:format)        {:controller=>"manage_bookings", :action=>"new"}
        edit_manage_manage_booking GET    /manage/:manage_id/bookings/:id/edit(.:format)   {:controller=>"manage_bookings", :action=>"edit"}
             manage_manage_booking GET    /manage/:manage_id/bookings/:id(.:format)        {:controller=>"manage_bookings", :action=>"show"}
                                   PUT    /manage/:manage_id/bookings/:id(.:format)        {:controller=>"manage_bookings", :action=>"update"}
                                   DELETE /manage/:manage_id/bookings/:id(.:format)        {:controller=>"manage_bookings", :action=>"destroy"}
                    pending_manage POST   /manage/pending(.:format)                        {:controller=>"manage", :action=>"pending"}
                                   GET    /manage/pending(.:format)                        {:controller=>"manage", :action=>"pending"}
                   upcoming_manage POST   /manage/upcoming(.:format)                       {:controller=>"manage", :action=>"upcoming"}
                                   GET    /manage/upcoming(.:format)                       {:controller=>"manage", :action=>"upcoming"}
                      manage_index GET    /manage(.:format)                                {:controller=>"manage", :action=>"index"}
                                   POST   /manage(.:format)                                {:controller=>"manage", :action=>"create"}
                        new_manage GET    /manage/new(.:format)                            {:controller=>"manage", :action=>"new"}
                       edit_manage GET    /manage/:id/edit(.:format)                       {:controller=>"manage", :action=>"edit"}
                            manage GET    /manage/:id(.:format)                            {:controller=>"manage", :action=>"show"}
                                   PUT    /manage/:id(.:format)                            {:controller=>"manage", :action=>"update"}
                                   DELETE /manage/:id(.:format)                            {:controller=>"manage", :action=>"destroy"}


Try:

<% form_for @new_room, :url => manage_rooms_path(@place)  do |f| %>

or maybe it will work this way:

<% form_for manage_rooms_path(@place, @new_room)  do |f| %>

@new_room is new instance of Room model, so in controller add:

@new_room = Room.new
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜