正在检测当前页面吗?在导航中部分不起作用 [英] detecting the current_page? in navigation partial dont work

查看:14
本文介绍了正在检测当前页面吗?在导航中部分不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是‘共享/子导航’部分的代码。当我点击一个链接时,它显示错误No route matches {:action=>"show", :controller=>"location"} ,但定义了路线。我认为下面的代码有问题。

-if current_page? location_path
  = link_to 'Edit Location', edit_location_path
-if current_page? user_path
  = link_to 'Edit User', edit_user_path
-if current_page? alert_path
  = link_to 'Edit Alert', edit_alert_path

以下是我的路线

location GET    /locations/:id(.:format)                        locations#show
user     GET    /users/:id(.:format)                            users#show
alert    GET    /alerts/:id(.:format)                           alerts#show

推荐答案

路由

底线是,由于您将您的路由定义为成员路由,因此您需要确保将适当的id传递给每个成员:

#config/routes.rb
resources :users, only: [:show, :edit]
resources :locations, only: [:show, :edit]
resources :alerts, only: [:show, :edit]

这意味着您必须将:id值传递给这些路由中的任何一个--这可以按如下方式完成:

user_path("2")

--

错误

错误显然是在此处创建的:

-if current_page? location_path

如上所述,您需要向路径传递一个有效的"id",以允许它获取所需的对象。您需要执行以下操作:

-if current_page? location_path("2")
不过,更紧迫的是您对这些方法的单独调用。当然,肯定有更好的方法来管理这种逻辑是如何定义的。我会尝试以下方法:

--

帮助者

我想我会做这样的帮手:

#app/helpers/your_helper.rb
Class YourHelper
   def edit_current(controller, object)
      current = controller.singularize
      return link_to "Edit #{current}", eval("edit_#{current}_path(object)")
   end
end

这应该允许您调用:

<%= edit_current(controller_name, @user) %>

这篇关于正在检测当前页面吗?在导航中部分不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆