好吧,我有一個rails應用程序,用戶可以在其中創建引腳。然後他們可以評論這些引腳。我該做什麼是刪除引腳url中的控制器名稱。從url中刪除控制器名稱時出錯
所以不是:http://localhost:3000/pins/name我http://localhost:3000/name
**我這樣做,在我的**的config/routes.rb中使用這個****
Rails.application.routes.draw do
resources :pins, :only => [:index, :new, :create], :path => '' do
resources :comments
member do
put 'upvote'
end
end
但現在,當我嘗試評論一針我有這樣的錯誤:
wrong constant name 'pin name'
,並從我comments_controller.rb此行FOM來的錯誤:
def load_commentable
resource, id = request.path.split('/')[1, 2]
@commentable = resource.singularize.classify.constantize.friendly.find(id)
end
任何想法如何解決這個問題?
編輯:
**rake routes** output:
pin_comments GET /:pin_id/comments(.:format) comments#index
POST /:pin_id/comments(.:format) comments#create
new_pin_comment GET /:pin_id/comments/new(.:format) comments#new
edit_pin_comment GET /:pin_id/comments/:id/edit(.:format) comments#edit
pin_comment GET /:pin_id/comments/:id(.:format) comments#show
PATCH /:pin_id/comments/:id(.:format) comments#update
PUT /:pin_id/comments/:id(.:format) comments#update
DELETE /:pin_id/comments/:id(.:format) comments#destroy
您能否提供'錯誤的常數名稱'pin name''錯誤的所有回溯? – 2015-03-02 23:20:04
+「rake routes」命令的輸出 – 2015-03-02 23:20:35
您需要爲此添加自定義路由,但要小心限制一個有效的引腳名稱,否則將與其他路由發生衝突。 – tadman 2015-03-02 23:21:56