简体   繁体   English

ruby on rails-不存在的路线

[英]ruby on rails- non-existent route

I have a controller file,我有一个控制器文件,

abc_controller.rb. 

I have defined the show method inside it.我已经在其中定义了 show 方法。

I have a view file,我有一个视图文件,

show.html.haml

inside app/views/abc/内部应用程序/视图/ abc/

In my routes.rb file, I am giving the following command在我的 routes.rb 文件中,我给出了以下命令

resources :abc

I have a button我有一个按钮

= link_to 'abc', abc_path, class: 'btn btn-default'

But when I click on the button, its not going to the new page.但是当我点击按钮时,它不会进入新页面。

I am getting non-existent route error.我收到不存在的路由错误。 Since I am a newbie to rails, I am not able to figure what the problem is.由于我是 Rails 的新手,我无法弄清楚问题是什么。

If you do resources (plural) the resulting route for show requires an id: /abc/:id(.:format) , so abc_path requires that you pass that :id or a object.如果你做resources (复数)显示的结果路由需要一个 id: /abc/:id(.:format) ,所以abc_path要求你传递 :id 或一个对象。 If you are dealing with a singular abc ( resource :abc ), the resulting path doesn't require that :id, so you code should work (this is less common, but hard to tell with your "abc" example.如果您正在处理单个 abc ( resource :abc ),则生成的路径不需要该 :id,因此您的代码应该可以工作(这不太常见,但很难用您的“abc”示例说明。

You are getting a error because there is no such path as abc_path .您收到一个错误,因为没有像abc_path这样的路径。 Run rake routes and you'll see the routes that Rails understands.运行rake routes ,你会看到 Rails 理解的路由。 In you example, resources :abc produces the following routes.在您的示例中, resources :abc生成以下路由。

abc_index GET      /abc(.:format)            abc#index
          POST     /abc(.:format)            abc#create
  new_abc GET      /abc/new(.:format)        abc#new
 edit_abc GET      /abc/:id/edit(.:format)   abc#edit
      abc GET      /abc/:id(.:format)        abc#show
          PATCH    /abc/:id(.:format)        abc#update
          PUT      /abc/:id(.:format)        abc#update
          DELETE   /abc/:id(.:format)        abc#destroy

The first column are the named routes.第一列是命名路由。 So in order to get to the index action of abc_controller , the route is named abc_index_path .所以为了到达abc_controllerindex动作,路由被命名为abc_index_path There is an abc_path but it needs an id params which means that you need to pass something to it.有一个abc_path但它需要一个id params 这意味着你需要向它传递一些东西。 In your case, there's no definitive value to pass to this method so as a trial just use abc_path(1) which will redirect you to /abc/1 .在您的情况下,没有确定的值可以传递给此方法,因此作为试用,只需使用abc_path(1)将您重定向到/abc/1 This goes to the show action with params[:id] set to 1 .这将转到params[:id]设置为1的 show 操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM