【发布时间】:2016-01-01 22:52:32
【问题描述】:
我已经开始将我的一个应用程序从 rails 3.x 移植到 rails 4.x....
当我在开发中启动应用程序时,我收到与路由定义相关的错误:
=> Booting WEBrick
=> Rails 4.2.5 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
/home/francesco/.rvm/gems/ruby-2.2.2@Best-i-gest_v2/gems/actionpack-4.2.5/lib/action_dispatch/routing/route_set.rb:549:in `add_route': Invalid route name, already in use: 'app_settings' (ArgumentError)
You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with `resources` as explained here:
http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
.....
这是我的 routes.rb 文件的一部分,其中包含标记为双重定义的路线:
.....
get 'app_settings' => 'admin/app_settings#index', :as => 'app_settings'
put 'app_settings' => 'admin/app_settings#index', :as => 'app_settings'
post 'app_settings' => 'admin/app_settings#index', :as => 'app_settings'
post 'app_settings/upload' => 'admin/app_settings#upload_logo', :as => 'app_settings/upload'
.....
我已经定义了这些路由,因为我将只使用“索引”操作来管理与应用程序设置相关的所有操作(应用程序有一个存储所有设置的数据库记录,该记录是在第一次自动创建的用户将加载页面,然后在保存时更新),如您在此处看到的:
# only one record here! it will store all the application settings
def index
# manages all the controller actions inside the index...
if request.get?
# this is a get request... returns the first settings record or a new one if none exists!
@app_settings = !AppSettings.all.first.nil? ? AppSettings.all.first : AppSettings.new
elsif request.post?
# this is a post request, the settings record will be created
@app_settings = AppSettings.new(params[:app_settings])
@app_settings.save
elsif request.put?
# this will update the existing app_settings record
@app_settings = AppSettings.find_by_id(params[:app_settings][:id].to_i)
@app_settings.update_attributes(params[:app_settings])
end
# renders the index page
render "index"
end
我正在寻找一种方法来更正 routes.rb 文件(保持我的控制器和视图不变!!)或另一种方法来管理这个问题!!
等待您的建议, 非常感谢您的宝贵时间,
弗朗西斯科
【问题讨论】:
标签: ruby-on-rails-3 ruby-on-rails-4 routes porting custom-routes