Rails:带有块和 GET 参数的 link_to?

发布于 2024-08-30 09:32:22 字数 531 浏览 11 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光与爱终年不遇 2024-09-06 09:32:22

link_to 采用相同的选项url_for 会这样做。 话虽如此,没有 :type 选项,并且它并不真正接受块,所以我的猜测是你的第二个示例起作用的原因是因为它位于 a 的范围内图书视图。 正如所述Tom 在对此答案的回复中,将块传递给 link_to 可以用作第一个参数(链接文本)的替换。

如果 Book 是一种资源,您可以通过向 link_to 帮助程序传递 Rails 为您提供的方便的自动生成资源路由之一来生成您要查找的 URL。在尝试此操作之前运行rake paths

<%= link_to "Edit", edit_book_path(book) %>

否则,您可以明确说明要链接到的控制器/操作:

<%= link_to "Edit", :controller => "books", :action => "edit", :id => book %>

Happy hacking。

编辑:差点忘了,您可以在声明要链接的对象的 id 之后添加绕过它们的查询字符串。

<%= link_to "Edit", edit_book_path(book, :query1 => "value", :query2 => "value")

将产生 /books/1/edit?query1=value&query2=value。或者:

<%= link_to "Edit", :controller => "books", :action => "edit", :id => book, :query1 => "value", :query2 => "value" %>

link_to takes the same options that url_for does. Having said that, there is no :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. As mentioned by Tom in a reply to this answer, passing a block to 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. Run rake routes before you try this:

<%= link_to "Edit", edit_book_path(book) %>

Otherwise, you can explicitly state what controller/action you want to link to:

<%= link_to "Edit", :controller => "books", :action => "edit", :id => book %>

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.

<%= link_to "Edit", edit_book_path(book, :query1 => "value", :query2 => "value")

Would product /books/1/edit?query1=value&query2=value. Alternatively:

<%= link_to "Edit", :controller => "books", :action => "edit", :id => book, :query1 => "value", :query2 => "value" %>
叫思念不要吵 2024-09-06 09:32:22

尝试以下方式

<% link_to(:edit, :type => 'book', :id => book) do %>
    ...
<% end %>

或实现相同的 url 使用

<% link_to(:action=>'edit', :type => 'book', :id => book) do %>
    ...
<% end %>

Try Follwing

<% link_to(:edit, :type => 'book', :id => book) do %>
    ...
<% end %>

or to achieve same url Use

<% link_to(:action=>'edit', :type => 'book', :id => book) do %>
    ...
<% end %>
伴随着你 2024-09-06 09:32:22

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 to link_to or book, and is sending it to book because it is closer to the block. book do ... end returns nil, so you're left with link_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

小鸟爱天空丶 2024-09-06 09:32:22

在 mime_types.rb 文件中添加:

Mime::Type.register "text/application", :book

in mime_types.rb file add:

Mime::Type.register "text/application", :book

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文