Rails:带有块和 GET 参数的 link_to?
如何在 link_to
块声明中实现查询字符串和 URL 参数?现在,我有这个,它可以工作:
<%= link_to 'Edit', :edit, :type => 'book', :id => book %>
上面的工作,和输出:
http://localhost:3000/books/edit/1?type=book
我想做的是这样的:
<% link_to :edit, :type => 'book', :id => book do %>
...
<% end %>
但是上面的格式输出:
http://localhost:3000/books/edit/
这不是我正在寻找的......我想要它像前面的例子一样输出一个 URL。
我怎样才能实现这个目标?
How can I achieve query string and URL parameters in a link_to
block declaration? Right now, I have this, which works:
<%= link_to 'Edit', :edit, :type => 'book', :id => book %>
The above works, and outputs:
http://localhost:3000/books/edit/1?type=book
What I want to do is something like this:
<% link_to :edit, :type => 'book', :id => book do %>
...
<% end %>
But the above format outputs:
http://localhost:3000/books/edit/
Which isn't what I'm looking for... I want it to output a URL like the previous example.
How can I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
link_to
采用相同的选项url_for
会这样做。话虽如此,没有正如所述Tom 在对此答案的回复中,将块传递给:type
选项,并且它并不真正接受块,所以我的猜测是你的第二个示例起作用的原因是因为它位于 a 的范围内图书视图。link_to
可以用作第一个参数(链接文本)的替换。如果 Book 是一种资源,您可以通过向
link_to
帮助程序传递 Rails 为您提供的方便的自动生成资源路由之一来生成您要查找的 URL。在尝试此操作之前运行rake paths
:否则,您可以明确说明要链接到的控制器/操作:
Happy hacking。
编辑:差点忘了,您可以在声明要链接的对象的 id 之后添加绕过它们的查询字符串。
将产生
/books/1/edit?query1=value&query2=value
。或者:link_to
takes the same options thaturl_for
does.Having said that, there is noAs mentioned by Tom in a reply to this answer, passing a block to:type
option and it doesn't really accept blocks, so my guess is that the reason the your second example works is because it's located within the scope of a Book view.link_to
can be used as a replacement for the first argument (the link text).If Book is a resource, you can get the
link_to
helper to generate the URL you're looking for by passing it one of the handy, automatically generated resource routes rails makes for you. Runrake routes
before you try this:Otherwise, you can explicitly state what controller/action you want to link to:
Happy hacking.
EDIT: Almost forgot, you CAN add query strings bypassing them in AFTER you declare the id of the object you're linking to.
Would product
/books/1/edit?query1=value&query2=value
. Alternatively:尝试以下方式
或实现相同的 url 使用
Try Follwing
or to achieve same url Use
Ruby 不知道您是将
do ... end
块发送到link_to
还是book
,而是将其发送到book
因为它离街区更近。book do ... end
返回nil
,因此您只剩下link_to :edit, :type=>'book', :id=>;无。您需要将参数括起来,当您使用它时,我会使用控制器、操作、id 设置重写它以使其更易于理解:
link_to{:controller=>"books",:action= >"编辑",:id=>书}做...结束
Ruby doesn't know if you're sending the
do ... end
block tolink_to
orbook
, and is sending it tobook
because it is closer to the block.book do ... end
returnsnil
, so you're left withlink_to :edit, :type=>'book', :id=>nil
. You will need to bracket the parameters, and while you're at it, I would rewrite it to be more understandable with a controller, action, id setup:link_to{:controller=>"books",:action=>"edit",:id=>book}do ... end
在 mime_types.rb 文件中添加:
Mime::Type.register "text/application", :book
in mime_types.rb file add:
Mime::Type.register "text/application", :book