Rails 在 routes.rb 里添加 match 配置典型路由后,不能使用 rails 命令

a307697028 · 2014年03月04日 · 最后由 pynix 回复于 2014年03月04日 · 5992 次阅读

我在 routes.rb 中添加:

match ':controller(/:action(/:id(.:format)))'

执行

rails g controller 命令报错

ubuntu@ubuntu:~/project/demo$ rails g controller www /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/actionpack-4.0.3/lib/action_dispatch/routing/mapper.rb:191:in normalize_conditions!': You should not use thematchmethod in your router without specifying an HTTP method. (RuntimeError) If you want to expose your action to both GET and POST, addvia: [:get, :post]option. If you want to expose your action to GET, usegetin the router: Instead of: match "controller#action" Do: get "controller#action" from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/actionpack-4.0.3/lib/action_dispatch/routing/mapper.rb:67:ininitialize' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/actionpack-4.0.3/lib/action_dispatch/routing/mapper.rb:1443:in new' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/actionpack-4.0.3/lib/action_dispatch/routing/mapper.rb:1443:inadd_route' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/actionpack-4.0.3/lib/action_dispatch/routing/mapper.rb:1422:in decomposed_match' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/actionpack-4.0.3/lib/action_dispatch/routing/mapper.rb:1403:inblock in match' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/actionpack-4.0.3/lib/action_dispatch/routing/mapper.rb:1394:in each' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/actionpack-4.0.3/lib/action_dispatch/routing/mapper.rb:1394:inmatch' from /home/ubuntu/project/demo/config/routes.rb:62:in block in <top (required)>' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/actionpack-4.0.3/lib/action_dispatch/routing/route_set.rb:341:ininstance_exec' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/actionpack-4.0.3/lib/action_dispatch/routing/route_set.rb:341:in eval_block' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/actionpack-4.0.3/lib/action_dispatch/routing/route_set.rb:319:indraw' from /home/ubuntu/project/demo/config/routes.rb:1:in <top (required)>' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:inload' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in block in load' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:214:inload_dependency' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in load' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.3/lib/rails/application/routes_reloader.rb:40:inblock in load_paths' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.3/lib/rails/application/routes_reloader.rb:40:in each' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.3/lib/rails/application/routes_reloader.rb:40:inload_paths' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.3/lib/rails/application/routes_reloader.rb:16:in reload!' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.3/lib/rails/application/routes_reloader.rb:26:inblock in updater' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/activesupport-4.0.3/lib/active_support/file_update_checker.rb:75:in call' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/activesupport-4.0.3/lib/active_support/file_update_checker.rb:75:inexecute' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.3/lib/rails/application/routes_reloader.rb:27:in updater' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.3/lib/rails/application/routes_reloader.rb:6:inexecute_if_updated' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.3/lib/rails/application/finisher.rb:69:in block in <module:Finisher>' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.3/lib/rails/initializable.rb:30:ininstance_exec' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.3/lib/rails/initializable.rb:30:in run' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.3/lib/rails/initializable.rb:55:inblock in run_initializers' from /home/ubuntu/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/tsort.rb:150:in block in tsort_each' from /home/ubuntu/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/tsort.rb:183:inblock (2 levels) in each_strongly_connected_component' from /home/ubuntu/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/tsort.rb:219:in each_strongly_connected_component_from' from /home/ubuntu/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/tsort.rb:182:inblock in each_strongly_connected_component' from /home/ubuntu/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/tsort.rb:180:in each' from /home/ubuntu/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/tsort.rb:180:ineach_strongly_connected_component' from /home/ubuntu/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/tsort.rb:148:in tsort_each' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.3/lib/rails/initializable.rb:54:inrun_initializers' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.3/lib/rails/application.rb:215:in initialize!' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.3/lib/rails/railtie/configurable.rb:30:inmethod_missing' from /home/ubuntu/project/demo/config/environment.rb:5:in <top (required)>' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.3/lib/rails/application.rb:189:inrequire' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.3/lib/rails/application.rb:189:in require_environment!' from /home/ubuntu/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.3/lib/rails/commands.rb:44:in' from bin/rails:4:in require' from bin/rails:4:in

'

錯誤訊息裡面有提示:

You should not use thematchmethod in your router without specifying an HTTP method. (RuntimeError)
If you want to expose your action to both GET and POST, addvia: [:get, :post]option.
If you want to expose your action to GET, usegetin the router:
Instead of: match "controller#action"

Rails 4 要聲明 http 動詞 => match 可傳入 via: 選項。 另外可以用 controller#action 的形式來寫路由,這種 match ':controller(/:action(/:id(.:format)))' 不推薦使用。

可以參考: 拥抱 Rails 4 —— 详述 Rails 4 的新变化 ActionDispatch::Routing::Mapper::Base

可能是因为来在 rails 中 match 已经被 deprecated 了。 试着用 get 或者 post 来代替 match

@alsotang 加入 via: [:get,:post] 选项可解决。

@Juanito 因为我是参照http://ihower.tw/rails3/basic.html 这个开始学习的,还是对你给我参考资料有很多不明白~~ - -!

#4 楼 @a307697028 rails4 里面使用 match 的话 必须指定 请求方式 post get。。。

match ':controller(/:action(/:id(.:format)))'

不要用这个,这是大忌。至于书太旧以至于没修正,你就先跳过吧…

#6 楼 @chitsaou 记住了。谢谢啦

需要 登录 后方可回复, 如果你还没有账号请 注册新账号