如果我们正在查看当前页面,我使用 :class =>('active' if current_page?desired_path) 将选定的标签添加到导航链接:
<%= link_to "Portfolio", portfolio_path, :class =>('active' if current_page?portfolio_path) %>
我希望此功能与指向已安装引擎的页面一起使用。从我的路线.rb:
mount Blogit::Engine => "/blog"
我想做这样的事情:
<%= link_to "Blog", blogit_path, :class =>('active' if current_page?blogit_path) %>
. . . 但显然 blogit_path 不是合适的路径,因为我的班级从未设置为活动状态。我也试过current_page?('/blog')
,因为我在网址http://myapp.com/blog - 但即使这样也行不通。
请注意,current_page?blog_root_path
只要我在博客引擎的路径内,它就可以工作。如果我在引擎之外,我会得到错误。
如何使用 current_page 引用已安装的引擎路径?
以下是相关rake routes
结果:
. . .
blogit /blog Blogit::Engine
root GET / home#index
portfolio_index GET /portfolio(.:format) portfolio#index
portfolio GET /portfolio(.:format) portfolio#index
Routes for Blogit::Engine:
GET /posts/page/:page(.:format) blogit/posts#index
post_comments POST /posts/:post_id/comments(.:format) blogit/comments#create
post_comment DELETE /posts/:post_id/comments/:id(.:format) blogit/comments#destroy
posts GET /posts(.:format) blogit/posts#index
POST /posts(.:format) blogit/posts#create
new_post GET /posts/new(.:format) blogit/posts#new
edit_post GET /posts/:id/edit(.:format) blogit/posts#edit
post GET /posts/:id(.:format) blogit/posts#show
PATCH /posts/:id(.:format) blogit/posts#update
PUT /posts/:id(.:format) blogit/posts#update
DELETE /posts/:id(.:format) blogit/posts#destroy
blog_root GET / blogit/posts#index