How to define a nested resources for polymorphic models with different namespace?
I am struggling with defining a route in routes.rb to work with nested resources while having a polymorphic association and models with different namespaces.
There are two controllers (I show important lines only):
/app/controllers/project/profile/likes_controller.rb
module Project
module Profile
class LikesController < Project::ApplicationController
def toggle
...
end
end
end
end
/app/controllers/project/place/profile/profiles_controller.rb
module Project
module Place
module Profile
class ProfilesController < Project::ApplicationController
...
end
end
end
end
... and two models, the first having a polymorphic association called likeable:
/app/models/project/profile/like.rb
module Project
module Profile
class Like < ActiveRecord::Base
...
belongs_to :likeable, :polymorphic => true
...
end
end
end
... and the second model being a likeable:
/app/models/project/place/base.rb
module Project
module Place
class Base < ActiveRecord::Base
has_many :likes, :as => :likeable, :class_name => "Project::Profile::Like", :dependent => :destroy
end
end
end
... and this is my route definition as is:
routes.rb
...
scope :module => "project" do
scope :module => "place" do
resources :places do
collection do
scope :module => "profile" do
resources :profiles do
resources :likes
end
end
end
end
end
scope :module => "profile" do
resources :likes do
collection do
post :toggle
end
end
end
end
...
As we can see, resource likes is a nested resource of resource places.
What I want to achieve?
I want to have a path to the nested resource place/likes, which should point to:
{:action=>"toggle", :controller=>"project/profile/likes"}
and keep the id of the likeable, place_id in this case.
I tried the following:
<%= button_to t(:user_likes), ([likeable, likeable.likes.new]), :action => 'toggle', ... %>
but this results in the following path:
project_place_profile_basis_project_profile_likes_path
which is not correct. The correct path would be project_profile_likes_path
.
The following routes are available (rake routes):
profile_likes POST
/places/profiles/:profile_id/likes(.:format)
{:action=>"index", :controller=>"porject/place/profile/likes"}
and
toggle_likes POST /likes/toggle(.:format)
开发者_如何学C {:action=>"toggle", :controller=>"project/profile/likes"}
Has anyone an idea how to get the path to the controller => project/profile/likes and action => toggle?
精彩评论