使用 Ruby on Rails link_to 链接到控制器操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8190240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:22:36  来源:igfitidea点击:

Using Ruby on Rails link_to to link to controller action

ruby-on-railsrubyruby-on-rails-3.1

提问by Hyman Franklin

I'd just started toying around with Ruby on Rails and had come across an issue with linking to another action in a controller from a particular view. I am almost certain it's an issue (or lack of code) in my routes.rbfile, but I think I'm misunderstanding exactly how this file works & what I have to do. I've got a solution but pretty sure it's not the "best way" to do it.

我刚刚开始玩弄 Ruby on Rails,并遇到了从特定视图链接到控制器中的另一个操作的问题。我几乎可以肯定这是我的routes.rb文件中的一个问题(或缺少代码),但我认为我误解了这个文件的工作原理以及我必须做的事情。我有一个解决方案,但很确定这不是实现它的“最佳方式”。

I have one controller called homewith two actions, index(which is the default) and newbill. Inside index.html.erbI have:

我有一个控制器调用home两个动作,index(这是默认值)和newbill. 里面index.html.erb我有:

<h1>Home View</h1>
<%= link_to "new", :controller => "home", :action => "newbill" %>

However I was getting a routing error:

但是我收到了一个路由错误:

No route matches {:controller=>"home", :action=>"newbill"}

Doing rake routesgives me the following:

rake routes给了我以下内容:

root  / {:controller=>"home", :action=>"index"}

I then (following some Googling) added this code to routes.rb

然后我(按照一些谷歌搜索)将此代码添加到 routes.rb

match 'home/newbill' => 'home#newbill', :as => :newbill

And then in my index.html.erbI've got this:

然后在我的index.html.erb我有这个:

<%= link_to "Name", newbill_path %>

And now this works as expected. My questions however are:

现在这按预期工作。然而,我的问题是:

  1. Why does this work? What exactly is going on behind the scenes?
  2. Surely this is not the best way to do it? Adding another match 'home/newbill'...for every controller / action I want to link to seems a rubbish way of doing things.
  1. 为什么这样做?幕后究竟发生了什么?
  2. 这肯定不是最好的方法吗?match 'home/newbill'...为我想要链接的每个控制器/动作添加另一个似乎是一种垃圾做事方式。

I really like Ruby, but struggling a bit with this aspect of Rails...routing in general is messing up my head a bit I think!

我真的很喜欢 Ruby,但是在 Rails 的这方面有点挣扎……我认为一般来说路由有点让我头疼!

Any help is much appreciated :D

非常感谢任何帮助:D

Thanks,

谢谢,

Hyman

Hyman

采纳答案by Brett Bender

You should check out the Rails Routing guide. A read through will help you understand what is going on behind the scenes.

您应该查看Rails Routing 指南。通读一遍将帮助您了解幕后发生的事情。

回答by sameera207

I guess the first time your code didn't work because your homecontroller is defined as a resource.

我猜您的代码第一次不起作用,因为您的home控制器被定义为资源。

If you define a controller as a resource in routes.rbfile it will support only 7 standard methods (according to REST architecture):

如果您将控制器定义为routes.rb文件中的资源,它将仅支持 7 种标准方法(根据 REST 架构):

index
new
create
show
edit
update
destroy

If you need any more custom routes you should add them manually, say in your case 'newbill', may go as:

如果您需要更多自定义路线,您应该手动添加它们,在您的情况下说“newbill”,可能是:

resources :home do
    collection do
      get :newbill
    end
end

But as per my understanding, your newbill method should go to billscontrollers new, method not in the home controller.

但根据我的理解,您的 newbill 方法应该转到bills控制器new,而不是主控制器中的方法。

You are right, Rails routes are little bit confusing (at least for me), but once you understand you can do lots of cool stuff.

你是对的,Rails 路由有点令人困惑(至少对我来说),但是一旦你理解了你就可以做很多很酷的事情。

Read here for the Rails official routes documentation:

在此处阅读 Rails 官方路线文档:

http://guides.rubyonrails.org/routing.html.

http://guides.rubyonrails.org/routing.html

回答by Scott Radcliff

This works becuase rails filters every request through the router looking for a match. This enables you to define custom routes such as domain.com/post when the path is actually blog#post. Prior to rails 3, a catch-all route was the last route in the routes file. This allowed you to define a controller and action and it would just work. I'm on my iPad and not near any projects, so I can't verify it, but I think that route is still there in rails 3.1, it just needs to be umcommented.

这是有效的,因为 rails 通过路由器过滤每个请求以寻找匹配项。这使您可以在路径实际上是 blog#post 时定义自定义路由,例如 domain.com/post。在 rails 3 之前,一个包罗万象的路由是路由文件中的最后一个路由。这允许你定义一个控制器和动作,它就可以工作了。我在我的 iPad 上并且不在任何项目附近,所以我无法验证它,但我认为该路线仍然存在于 rails 3.1 中,只需要对其进行注释。