我正在尝试在 2.3.11 Ruby on Rails 应用程序中使用 URL 帮助程序,但无论我尝试什么解决方案,我都会遇到许多问题...
在尝试获取路径之前,我已经阅读了很多“解决方案”,例如包含 include ActionController::UrlWriter
,但每次都会遇到相同的错误:
>> app.users_path
NameError: undefined method `new_suggestion_path' for class `ActionController::Integration::Session'
我找不到这个问题。我有一个“建议” Controller 和一个"new"操作...是否有其他方法可以解决此问题并从控制台和/或 Rake 获取 URL ?
最佳答案
您需要为您尝试访问的命名路由定义资源路由。
要使建议路径在 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"
>>
关于ruby-on-rails - 在 Ruby on Rails 2.3.11 中使用控制台中的 URL 帮助程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25510451/