Rails link_to_if 问题
我有一个帮助方法,设计为根据用户所在的当前页面生成指向特定路径的链接。基本上,站点范围内的链接应指向 items_path,除非用户位于用户页面上。因此,我试图找出一些 DRY 逻辑,但我一直遇到麻烦:
def items_link(title, options = {}, html_options = {})
path = request.path
case path
when users_path,items_path
options = request.parameters.merge(options)
end
link_to_if(path == users_path, title, users_path(options), html_options) do
link_to(title, items_path(options), html_options)
end
end
使用此解决方案 items_path 会抛出 No Route matches
错误,尽管路线是正确的。 users_path 工作正常,直到我用 link_to 切换 link_to_if 路径。
link_to_if(path == items_path, title, items_path(options), html_options) do
link_to(title, users_path(options), html_options)
end
所以我猜我的问题出在 link_to_if 中的某个地方。我很接近吗?我当前的工作解决方案是:
def items_link(title, options = {}, html_options = {})
path = request.path
case path
when users_path
options = request.parameters.merge(options)
link_to(title, users_path(options), html_options)
when items_path
options = request.parameters.merge(options)
link_to(title, items_path(options), html_options)
else
link_to(title, users_path(options), html_options)
end
end
这工作正常,只是很难看。
更新:
我花了更多的时间,认为我必须进一步分解它,这实际上在另一个领域帮助了我,我喜欢有 link_action 助手。
def items_link(title, options = {}, html_options = {})
link_to(title, items_link_action(options), html_options)
end
def items_link_action(options = {})
path = request.path
case path
when users_path,items_path
options = request.parameters.merge(options)
end
if path == users_path
users_path(options)
else
items_path(options)
end
end
I have a helper method designed generate a link to a certain path depending on the current page the user is on. Basically, site wide the link should point to items_path, unless the user is on the user page. So, I'm trying to work out some DRY logic, but I keep running into trouble:
def items_link(title, options = {}, html_options = {})
path = request.path
case path
when users_path,items_path
options = request.parameters.merge(options)
end
link_to_if(path == users_path, title, users_path(options), html_options) do
link_to(title, items_path(options), html_options)
end
end
With this solution items_path throws a No route matches
error, although the route is correct. users_path works fine, until I switch around the link_to_if path with link_to's.
link_to_if(path == items_path, title, items_path(options), html_options) do
link_to(title, users_path(options), html_options)
end
So I am guessing my problem is somewhere in link_to_if. Am I close? My current working solution is:
def items_link(title, options = {}, html_options = {})
path = request.path
case path
when users_path
options = request.parameters.merge(options)
link_to(title, users_path(options), html_options)
when items_path
options = request.parameters.merge(options)
link_to(title, items_path(options), html_options)
else
link_to(title, users_path(options), html_options)
end
end
This works fine, it's just ugly.
Update:
I spent some more time and figured I had to break it out a little more, and this actually helped me in another area, I like having the link_action helper.
def items_link(title, options = {}, html_options = {})
link_to(title, items_link_action(options), html_options)
end
def items_link_action(options = {})
path = request.path
case path
when users_path,items_path
options = request.parameters.merge(options)
end
if path == users_path
users_path(options)
else
items_path(options)
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将相同的选项放入任一链接是否有意义?这可能是“无路由匹配”错误的根源。
我会看看 current_page?助手和 link_to_unless_current 助手。
类似这样的内容将显示指向用户索引操作的链接,除非它们已经位于用户索引操作处。可以对项目页面执行相同的操作。不确定这是否是您正在寻找的。如果是我,我只会在布局或共享部分中放置两个链接:
Does it make sense to be throwing the same options into either link? That may be the source of your No Route Matches error.
I would look at the current_page? helper and the link_to_unless_current helpers.
Something like this would show a link to the users index action unless they are already at the users index action. The same could be done for the items page. Not sure if this is what you were looking for though. If it were me I'd just drop two links in my layout or a shared partial:
这是我所得到的最接近的结果,我认为效果很好。
This is as close as I got, works pretty well I think.