Rails中的路由功能是如何对应的?

2019-03-25 13:38|来源: 网路

我才开始接触ROR,我是参照agile web development with rails这本书学习,按照上面的例子走,但是我发现我请求的地址似乎和config/routes.rb里面设定并不一样,比如说routes中是这样设置的:
map.connect ':controller/:action/:id'

可是我在浏览器中编辑的时候显示的是:http://localhost:3000/products/2/edit

2是id编号,Edit是action名,这样不是和设置不一致么,为什么照样可以运行?
而显示页面的地址是:http://localhost:3000/products/2, 默认不是找index么?怎么controller后面直接跟id了呢?

这个机制到底是什么样的呢? 还请知道的帮我解惑,先谢过了.





相关问答

更多
  • 我发现了这个问题。 我需要使用可选的通配符: get 'path/to/ember-root/(*ember-stuff-ignored-by-rails)', to: 'your_controller#your_aciton' I found the question. I need to use an optional wildcard: get 'path/to/ember-root/(*ember-stuff-ignored-by-rails)', to: 'your_controller#you ...
  • 用你的PHP例子,通过查看给定的路径profile.php找到页面。 该文件是由您的网络服务器搜索的(如果找到)执行的。 在Rails中,URL与路由匹配以找到相应的控制器。 在你的Rails示例中, blog被映射到BlogController 。 现在Rails知道包含控制器的文件可以作为apps/controllers/blog_controller.rb 。 每个控制器都有动作,所以show部分与BlogController的show动作相匹配,这是由控制器中的show方法表示的。 有关Rails路 ...
  • 如果你使用Rails.application.routes.draw而不是Rails.application.routes.append你应该可以使用reload_routes! 方法来清除内容。 在使用append之后调用reload时,它实际上只是重新加载了您已添加的路由。 如果您绘制路线,它将永远不会附加到路线对象,您应该没问题。 If you use Rails.application.routes.draw rather than Rails.application.routes.append y ...
  • Matt(之前的答案作者)几乎回答了这个问题,只是想注意你也可以在你的路线上添加as选项给它一个名字: match '/load_data', to: 'routes#new', via: 'get', as: 'routes' 这将为您“定义” routes_path 。 Matt (the previous answer author) pretty much answered the question, just want to notice that you can also append the ...
  • 当您声明as: 'patient'您可以在视图中引用此路线: patient_path或patient_url ,换句话说,它是一条命名路线。 When you declare as: 'patient' you can reference this route in you view as: patient_path or patient_url, in other words it's a named route.
  • 那这个呢: <% link_to @post.title, post_path if post_path %> What about this: <% link_to @post.title, post_path if post_path %>
  • 您可以在config / routes.rb中使用globbing: get "/api/*path", to: "api#index" 可以通过params[:path]在控制器中访问 详细信息可在导轨指南中找到。 You can use globbing in your config/routes.rb: get "/api/*path", to: "api#index" Which would be accessible in the controller via params[:path] De ...
  • 如果要为模型生成默认路径,而不执行新操作,则可以在路径文件中使用此指令 resources :model, except: :new 在同一个文件中,您将为新操作添加非托管(即“手动”)路径 put "articles/new" => "articles#new" 注意:你也可以使用这个语法match "articles/new" => "articles#new", via: :put使你可以在将来轻松添加更多方法 更多信息: http : //guides.rubyonrails.org/routi ...