link_to ruby​​中的表单

时间:2011-10-12 08:57:14

标签: ruby-on-rails ruby

我想使用链接功能指向表单。 即<%= link_to'Reports','/ report / index.html.erb'%> 但这给了我一个错误,说没有路线匹配'/reports/index.html.erb。

谢谢, 拉姆亚。

2 个答案:

答案 0 :(得分:2)

链接应从服务器的角度指向表单。试试<%= link_to 'Reports', '/reports/index.html' %> (不使用'.erb')

确保您的路线确实定义了该网址。我猜它可能是'/ reports /'而不是'/reports/index.html',但是YMMV。

查阅命令rake routes的输出以查看定义了哪些路由。

答案 1 :(得分:2)

Rails不喜欢在URL中使用文档格式,除非有必要(比如一个动作可以处理多种请求格式)。如果你有reports/index.html.erb,那么它的路线就像其中一个:

match 'reports' => 'reports#index' #=> 'yourdomain.com/reports'
match 'reports/index' => 'reports#index' #=> 'yourdomain.com/reports/index

然后您的链接将是:

<%= link_to 'Reports', 'reports' %>

<%= link_to 'Reports', 'reports/index' %>

如果你真的想拥有.html,你可能会这样做:

match 'reports/index.html' => 'reports#index' #=> 'yourdomain.com/reports/index.html

match 'reports/index.:format' => 'reports#index' #=> 'yourdomain.com/reports/index.html

.html在第一种情况下毫无意义,在第二种情况下则没有必要。我不建议这样做,因为它不是标准的Rails实践。

高度建议您在继续前进之前阅读this tutorial on Routing,至少是前几节。它是Rails中绝对必不可少的一部分,如果你不理解它是如何工作的,那么你将永远不会成为一个高效的Rails程序员。