2012-03-17 1 views
6

저는 정말 간단한 문제가 있습니다. 나는 한 페이지의 보고서를 가지고 있으며 각 보고서에는 자체 탭이 있습니다. 어떤 탭을 강조 표시할지 결정하려면 current_page?을 사용하고 있습니다. 보고서를 제출할 때 current_page?은 더 이상 작동하지 않는 것 같습니다. 요청 방법이 POST이기 때문입니다.레일 current_page? 메서드가 POST 일 때 "실패"합니다.

이것은 의도 한 동작입니까 current_page? 나는 그것이 어째서 그런지 상상하기가 어렵습니다. 그것이 그렇다면, 사람들은이 문제를 어떻게 정상적으로 극복합니까?

는 여기 current_page? 호출의 예 : 나는 약 5 분 현상금을 넣어 후에 내 자신의 질문에 대한 답을 알아 낸 것처럼

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

+1

당신이 CURRENT_PAGE를 사용하는 방법을 포함하여, 귀하의 질문에 오류를 추가 할 수 있습니다

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 

을 지금 당신은이 작업을 수행 할 수 있습니다 ? – AMIT

+0

내 'current_page?'호출을 포함하도록 내 질문을 편집합니다. 나는 오류가 발생하지 않습니다. 그냥 거짓을 반환합니다. –

답변

9

좋아, 그것은 보인다. current_page?은 항상 POST에 false를 반환합니다. 여기

current_page?의 소스 코드입니다 : 그들은 단지 GET 요청 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 

난 정말 이해하지 않지만, 적어도 지금은 그건 알고 그것이있는 방법.

+0

아마도 답을 받아 들여서 현상금을 제거해야합니다. –

+0

내가 볼 수 있듯이 한 시간 전에 질문에 답했기 때문에 아직 그 일을 할 수 없습니다. –

+4

다른 사람들이 내가 갖고있는 것과 같은 좌절감을 가지고 있다면, "current_page?" "problem"에 대한 해결 방법을 발견했습니다 : http://stackoverflow.com/questions/5186613/rails-current-page-versus-controller-controller -이름 –

0

POST을 사용할 때 동일한 문제가 발생했습니다. link_path 그냥 여기서 내 솔루션이

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 

그런 짓을했다 url_for(action: 'action', controller: 'controller')

2
당신의 새로운 current_path? 방법을 만들 수

당신의 ApplicationHelper :

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? 도우미 메서드 만들 수 있습니다 : 하나 이상의 컨트롤러 + 행동

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'}) 

== 업데이트 ==는

이 좋아, 좀 덜 장황하지 REQ에 의해 current_request?를 사용하기로 결정 당신은 여러 작업과 일치하려고하는 컨트롤러 타이프 것을 uiring :

current_request?(controller: 'users', action: ['new', 'create']) 
관련 문제