【问题标题】:Rails: How to determine what page user is viewing?Rails:如何确定用户正在查看哪个页面?
【发布时间】:2014-01-06 14:23:59
【问题描述】:

例如,我有一个页脚

<%= link_to 'Tweets', tweets_path %>
<%= link_to 'Blogs', blogs_path %>

在推文索引页面中,我想隐藏&lt;%= link_to 'Tweets', tweets_path %&gt;。并展示其他东西。我怎么知道用户当前在resource 上?

具体来说,我想

resources = ['Tweet', 'Blog']   # get the model names, and there maybe something more to be added later
resources.each do  |resource|
  if controller.controller_name = resource  &&  controller.method_name = 'index'
      link_to new_resource_path  # for example, link_to new_tweet_path
  else 
      link_to resource_path   # for example, link_to tweets_path 
  end
end

粗略的想法在上面。但是在controller.controller_namelink_to方法中,不知道具体写法。

我从Can I get the name of the current controller in the view? 找到controller.controller_name 有什么好的方法可以做到这一点?

更新:

def footer_helper
  resources = ['tweet', 'blog']   # and perhaps something more
  resources.each do  |resource|
     if current_page?(controller: resource.pluralize, action: 'index')
        link_to "New #{resource.humanize}", {controller: resource.pluralize, action: 'new'}
      else
        link_to "#{resource.pluralize.humanize}", {controller: resource.pluralize, action: 'index'}
     end
  end
end

结束

现在我已经把它变成了像上面这样的助手。但是我觉得.pluralize.humanize 很烦人,有什么办法可以摆脱它们吗?

另外,如何在视图中使用它?当我使用&lt;%= footer_helper %&gt; 时,它显示["tweet", "blog"]。它无法正常返回。

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    params[:action] 也会向您显示它们被路由的动作,同样params[:controller] 会找出该动作在哪个控制器中。您可以使用它们为您的页脚编写一些逻辑。

    【讨论】:

      【解决方案2】:

      使用current_page? 助手:

      resources = ['tweet', 'blog']
      resources.each do  |resource|
        if current_page?(controller: resource, action: 'index')
          link_to(resource.humanize, { controller: resource, action: 'new' })
        else 
          link_to(resource.humanize, { controller: resource, action: 'index' })
        end
      end
      

      这可以通过 link_to_if 帮助器改进:

      resources = ['tweet', 'blog']
      resources.each do  |resource|
        link_to_if(current_page?(controller: resource, action: 'index'), resource.humanize, {controller: resource, action: 'new'}) do
          link_to(resource.humanize, {controller: resource, action: 'index'})
        end
      end
      

      如果您不想要计算机生成的界面文本(这通常是个坏主意),请考虑将资源设为 Hash,如下所示:

      resources = {'tweets' => "Tweet", 'blogs' => "Blog"}
      resources.each do  |resource, name|
        link_to_if(current_page?(controller: resource, action: 'index'), name, {controller: resource, action: 'new'}) do
          link_to(name, {controller: resource, action: 'index'})
        end
      end
      

      【讨论】:

      • 如何将new_resource_path 传递给link_to?我的意思是,resource 在这里只是一个字符串。但我需要将path 传递给link_to 方法
      • 抱歉,忽略了这一点。已编辑!
      • 现在我正试图将它变成一个 Helper 并在视图中使用它。但是输出有问题,你能帮我解决这个问题吗?我把它放在了 UPDATE
      • »但是我发现 .pluralize 和 .humanize 很烦人,有什么办法可以摆脱它们?« 嗯,是的,不尝试通过代码创建界面文本。请参阅我的帖子中的更新。
      【解决方案3】:

      它有一个很好的 gem,用于整洁的代码:

      https://github.com/robotmay/link_to_active_state

      这个 gem 为默认的 Rails link_to view helper 添加了一些额外的功能。它提供了一种非常简单的方法,可以根据当前路径向链接添加类。

      看看吧。

      【讨论】:

        【解决方案4】:

        我用这个方法:

        # application_helper.rb
        
        module ApplicationHelper
          def body_id
            [body_class, params[:action]].join('-')
          end
        
          def body_class
            controller.class.to_s.gsub('Controller', '').underscore.dasherize.gsub('/', '-')
          end
        end
        

        在我的布局中:

        # application.html.erb
        
        <body id="<%= body_id %>" class="<%= body_class %>">
        </body>
        

        所以对于TweetsController#index,这个渲染

        <body id="tweets-index" class="tweets">
        

        现在您可以根据用户所在的控制器或控制器操作应用 CSS:

        body#tweets-index a.tweet-links {
          display: none;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-05-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-06-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多