【问题标题】:Rails current_page? "fails" when method is POSTRails current_page?方法为 POST 时“失败”
【发布时间】:2012-03-17 11:58:37
【问题描述】:

我有一个非常简单的问题。我有一页报告,每个报告都有自己的标签。我正在使用current_page? 来确定应该突出显示哪个选项卡。当我提交任何报告时,current_page? 似乎不再起作用,显然是因为请求方法是POST

这是current_page? 的预期行为吗?我很难想象为什么会这样。如果是,人们通常如何解决这个问题?

以下是current_page? 调用的示例:

<li><%= link_to "Client Retention", reports_client_retention_path, :class => current_page?(reports_client_retention_path) ? "current" : "" %></li>

【问题讨论】:

  • 您能否将错误添加到您的问题中,包括您如何使用 current_page?
  • 我编辑我的问题以包括我的current_page? 电话。我没有收到错误。它只是返回 false。

标签: ruby-on-rails-3


【解决方案1】:

好吧,看起来我在提出赏金后大约 5 分钟就找到了自己问题的答案。看起来current_page?POST 上总是会返回 false。

这是current_page?的源代码:

# File actionpack/lib/action_view/helpers/url_helper.rb, line 588
def current_page?(options)
  unless request
    raise "You cannot use helpers that need to determine the current "                  "page unless your view context provides a Request object "                  "in a #request method"
  end

  return false unless request.get?

  url_string = url_for(options)

  # We ignore any extra parameters in the request_uri if the
  # submitted url doesn't have any either. This lets the function
  # work with things like ?order=asc
  if url_string.index("?")
    request_uri = request.fullpath
  else
    request_uri = request.path
  end

  if url_string =~ %r^\w+:\/\//
    url_string == "#{request.protocol}#{request.host_with_port}#{request_uri}"
  else
    url_string == request_uri
  end
end

我真的不明白为什么他们会特意让current_page? 只为GET 请求工作,但至少现在我知道它是这样的。

【讨论】:

  • 您可能应该接受您的回答,并移除赏金。
  • 我还不能这样做,因为如您所见,我只在一小时前回答了这个问题。
  • 如果其他人有同样的挫败感,我在这里找到了current_page?“问题”的解决方法:stackoverflow.com/questions/5186613/…
  • 我也对这个限制感兴趣,所以我找到了引入它的提交 - github.com/rails/rails/commit/…。不幸的是,它并没有解释太多。
【解决方案2】:

您可以在 ApplicationHelper 中创建一个新的 current_path? 方法:

def current_path?(*paths)
  return true if paths.include?(request.path)
  false
end

传入一个或多个路径,如果有匹配用户当前路径,则返回true:

current_path?('/user/new')
current_path?(root_path)
current_path?(new_user_path, users_path '/foo/bar')

或者,您可以创建一个新的 current_request? 辅助方法来检查 Rails 控制器 + 操作:

def current_request?(*requests)
  return true if requests.include?({
    controller: controller.controller_name,
    action: controller.action_name
  })
  false
end

传入一个或多个控制器+动作,如果有任何匹配用户当前请求,则返回true:

current_request?(controller: 'users', action: 'new')
current_request?({controller: 'users', action: 'new'}, {controller: 'users', action: 'create'})

==UPDATE==

好的,我决定让current_request? 的使用不那么冗长,因为在您尝试匹配多个操作时不需要输入控制器:

def current_request?(*requests)
  requests.each do |request|
    if request[:controller] == controller.controller_name
      return true if request[:action].is_a?(Array) && request[:action].include?(controller.action_name)
      return true if request[:action] == controller.action_name
    end
  end
  false
end

现在你可以这样做了:

current_request?(controller: 'users', action: ['new', 'create'])

【讨论】:

    【解决方案3】:

    我在使用POST 时遇到了同样的问题。我的解决方案是做这样的事情

    def menu_item link_text, link_path
      link_class = (request.original_url.end_with? link_path) ? 'active' : ''
      content_tag :li, link_to(link_text, link_path), class: link_class
    end
    

    其中link_path 就是url_for(action: 'action', controller: 'controller')

    【讨论】:

      猜你喜欢
      • 2015-01-04
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 2017-05-16
      • 2012-06-27
      • 1970-01-01
      • 2011-04-29
      相关资源
      最近更新 更多