Link_to 带有附加变量
我想创建一个带有两个附加变量的简单 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样的事情绝对应该重构为一个帮助器,例如
这是您可以做的使视图代码不那么可怕的最低限度。
您可能还想考虑将整个收集内容放入模型中。
另外,在这种情况下,
Hash#merge
可能会有所帮助,您可以这样做Things like this should definitely be refactored into a helper, such as
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不太优雅,但应该可以:
如果
users2
为空,h
参数将从生成的 URL 中省略。作为替代方案,您可以从选项哈希中过滤掉空白值:
Not very elegant, but should work:
If
users2
is blankh
parameter will be omitted from generated URL.As alternative you can filter out blank values from options hash: