Link_to 带有附加变量

发布于 2024-12-24 20:01:45 字数 516 浏览 4 评论 0原文

我想创建一个带有两个附加变量的简单 link_to (rails 3):

= link_to 'Try', new_try_path(:k => users.collect{|m| m.user.username}, :h=> users2.collect{|m| m.user2.username2}, :proof => true)

问题是如果 users2 为空,则会生成以下 html 代码: &k=[1]&&proof=true

我尝试过这样的事情。你能帮我吗?

= link_to 'Try', new_try_path(:k => users.collect{|m| m.user.username}, :h=> users2.collect{|m| m.user2.username2} if users2.blank?, :proof => true)

谢谢你!

I want to create a simple link_to (rails 3) with two additional variables:

= link_to 'Try', new_try_path(:k => users.collect{|m| m.user.username}, :h=> users2.collect{|m| m.user2.username2}, :proof => true)

The problem is if users2 is blank, this html code is generated: &k=[1]&&proof=true

I tried something like this. Can you help me please?

= link_to 'Try', new_try_path(:k => users.collect{|m| m.user.username}, :h=> users2.collect{|m| m.user2.username2} if users2.blank?, :proof => true)

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

云淡月浅 2024-12-31 20:01:45

像这样的事情绝对应该重构为一个帮助器,例如

# view
= try_link(users, users2)

# helper
def try_link(users, users2)
  options = { :k => users.collect { |m| m.user.username }, :proof => true }
  unless users2.blank?
    options[:h] = users2.collect { |m| m.user2.username2 }
  end

  link_to 'Try', new_try_path(options)
end

这是您可以做的使视图代码不那么可怕的最低限度。

您可能还想考虑将整个收集内容放入模型中。

另外,在这种情况下,Hash#merge可能会有所帮助,您可以这样做

a = { :foo => 1 }
b = { :bar => 2 }
puts a.merge(b)  # =>   { :foo => 1, :bar => 2 }

Things like this should definitely be refactored into a helper, such as

# view
= try_link(users, users2)

# helper
def try_link(users, users2)
  options = { :k => users.collect { |m| m.user.username }, :proof => true }
  unless users2.blank?
    options[:h] = users2.collect { |m| m.user2.username2 }
  end

  link_to 'Try', new_try_path(options)
end

This is about the bare minimum you can do to make the view code less horrible.

You might also want to consider putting the whole collect thing into the model.

Also Hash#merge might be helpful in cases like this, where you can do

a = { :foo => 1 }
b = { :bar => 2 }
puts a.merge(b)  # =>   { :foo => 1, :bar => 2 }
贵在坚持 2024-12-31 20:01:45

不太优雅,但应该可以:

- options = { :k => users.map{ |m| m.user.username }, :proof => true }
-# add :h parameter only if users2 is not empty
- options[:h] = users2.map{ |m| m.user2.username2 } unless users2.blank? 
= link_to 'Try, new_try_path(options)

如果 users2 为空,h 参数将从生成的 URL 中省略。

作为替代方案,您可以从选项哈希中过滤掉空白值:

# for ruby 1.9 (select only non-blank values)
options.select! { |k, v| v.present? }

# for ruby 1.8 (delete blank values)
options.delete_if { |k, v| v.blank? }

Not very elegant, but should work:

- options = { :k => users.map{ |m| m.user.username }, :proof => true }
-# add :h parameter only if users2 is not empty
- options[:h] = users2.map{ |m| m.user2.username2 } unless users2.blank? 
= link_to 'Try, new_try_path(options)

If users2 is blank h parameter will be omitted from generated URL.

As alternative you can filter out blank values from options hash:

# for ruby 1.9 (select only non-blank values)
options.select! { |k, v| v.present? }

# for ruby 1.8 (delete blank values)
options.delete_if { |k, v| v.blank? }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文