Rails 关于 Rails 路由,自定义添加后,报 “ActionController::RoutingError (No route matches {:action=>xxx,:controller=>"xxxx})"“

eric_chao · 2017年08月16日 · 最后由 djname 回复于 2017年09月07日 · 2131 次阅读

学习运用了一段时间的 Rails,对一些细节,还是不是太明白,特别对路由这块理解还不够深入,请大神指正

%span
  = link_to 'DownLoadFile',down_load_view_search_path
resources :searches do
  member do
    get 'down_load_file'
    get 'down_load_view'
  end    
end 

会报错,找不到具体路由,我很奇怪,配置了,但是找不到,但是不明白,不明确为什么会这样,找不到自己错在哪里。


ActionController::RoutingError (No route matches {:action=>"down_load_view", :controller=>"searches"}):
  app/views/searches/index.html.haml:14:in `block in _app_views_searches_index_html_haml___72676910611027597_71052940'
  app/views/searches/index.html.haml:3:in `_app_views_searches_index_html_haml___72676910611027597_71052940'


member 换成 collection

:member 是对单个实体进行操作,创建路由格式是: /:controller/:id/:your_method

:collection 是对实体集合进行操作,创建路由格式是: /:controller/:your_method

有一种疑问,Rails 世界中,在 view 层,单单只想跳转到另外一个界面,而这个页面不是和 edit,show,无关,只是跳转到一个新的页面,也得配置路由,我只想让这个路由,起到跳转的作用,不知道该如何实现。就是 link,只是简单的跳转到指定页面,在 Rails 中,不会了。

那你这么写吧, #隔开 controller action

post "/popup_signup_log" => "index#popup_signup_log"

get "/invitation_new" => "index#invitation_new"

@will_c_j 首先 谢谢您的指点,我先说明一下我的状态,用了 Rails 半年多,在工作中,增加新功能,修改修改以前的 BUG 什么的都还好,但是最近,发现往深的看,自己什么都不懂,好多东西想不明白。 (前面大白话)您上面的写的这种格式,我了解,写过,测试了,可以的。我想请教,这里的 Rails 工作原理,是 View 发送 request,进入路由,寻找相应的 controller 对应的 def,处理完成后,跳转对应的页面。 (我想问,创建自定义路由后,对应的 template,也必须创建?DispatchServlet 自动寻找该页面,无关乎 def 里面的是否 render 或者 redirect_to)

如果你的 action 里没有 render 或 redirect_to 会默认去找与你 action 同名的 template

不是必须创建 template, 你可以在你写的 action 里做 render 或 redirect_to,指定跳转

will_c_j 回复

谢谢,这个概念。弄懂了,歇歇您的指点,上面配置的‘post "/popup_signup_log" => "index#popup_signup_log" ...... 不能配置 member 换成 collection 这种形式么?没搞懂这种的区别,在官网看到对应的概念‘添加成员路由’,就是这种形式的,有什么区别,现在我只用您用的第二种形式实现了跳转‘/.,....=>..’

可以配置成那样的形式,你可以看看这个 https://ihower.tw/rails/routing.html

eric_chao 回复

是浏览器发送请求 然后匹配路由 在调用相应的 controller 中的 action 在 action 中返回 view view 渲染成最终浏览器中的页面

action route view 对应 你随便挑

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