我正在尝试在2.3.11 Ruby on Rails应用程序中使用URL帮助程序,但无论我尝试什么解决方案,我都面临很多问题......
在尝试获取路径之前,我已经阅读了许多“解决方案”,例如包括include ActionController::UrlWriter
,但每次都会遇到相同的错误:
>> app.users_path
NameError: undefined method `new_suggestion_path' for class `ActionController::Integration::Session'
我找不到这个问题。我有一个“建议”控制器和一个“新”操作...有没有其他方法来解决这个问题,并从控制台和/或Rake获取一个URL?
答案 0 :(得分:3)
您需要为要尝试访问的命名路由定义资源路由。
要在Rails 2.3中提供建议路径,请添加资源路由config/routes.rb
:
ActionController::Routing::Routes.draw do |map|
map.resources :suggestions
end
这将定义以下路线:
suggestions GET /suggestions(.:format) {:controller=>"suggestions", :action=>"index"}
POST /suggestions(.:format) {:controller=>"suggestions", :action=>"create"}
new_suggestion GET /suggestions/new(.:format) {:controller=>"suggestions", :action=>"new"}
edit_suggestion GET /suggestions/:id/edit(.:format) {:controller=>"suggestions", :action=>"edit"}
suggestion GET /suggestions/:id(.:format) {:controller=>"suggestions", :action=>"show"}
PUT /suggestions/:id(.:format) {:controller=>"suggestions", :action=>"update"}
DELETE /suggestions/:id(.:format) {:controller=>"suggestions", :action=>"destroy"}
现在您可以在控制台中使用这些路线:
>> app.new_suggestion_path
=> "/suggestions/new"
>>