2014-05-09 2 views
0

저는 여기 바보 같은 짓을하고 있습니다. 내보기에서경로가 일치하지 않습니다. {: action => "edit", : controller => "blogs"}

나는 다음과 같은 코드를 가지고 : 나는 페이지의 일부 페이지 특정 기능을 표시하는 데 사용하는

- if current_page?(edit_blog_path) 

.

ActionController::RoutingError - No route matches {:action=>"edit", :controller=>"blogs"}: 
    actionpack (3.2.16) lib/action_dispatch/routing/route_set.rb:540:in `raise_routing_error' 
    actionpack (3.2.16) lib/action_dispatch/routing/route_set.rb:536:in `rescue in generate' 
    actionpack (3.2.16) lib/action_dispatch/routing/route_set.rb:528:in `generate' 
    actionpack (3.2.16) lib/action_dispatch/routing/route_set.rb:569:in `generate' 
    actionpack (3.2.16) lib/action_dispatch/routing/route_set.rb:594:in `url_for' 
    actionpack (3.2.16) lib/action_dispatch/routing/url_for.rb:148:in `url_for' 
    actionpack (3.2.16) lib/action_view/helpers/url_helper.rb:107:in `url_for' 

내 레이크 노선

는 말한다 :

이 다음과 같은 오류가 발생

% rake routes | grep blog 
       blogs GET /blogs(.:format)     blogs#index 
        POST /blogs(.:format)     blogs#create 
      new_blog GET /blogs/new(.:format)    blogs#new 
      edit_blog GET /blogs/:id/edit(.:format)  blogs#edit 
       blog GET /blogs/:id(.:format)    blogs#show 
        PUT /blogs/:id(.:format)    blogs#update 
        DELETE /blogs/:id(.:format)    blogs#destroy 
      blogs_new POST /blogs/new(.:format)    blogs#new 
        GET /blogs/:id/next(.:format)  blogs#next 
        GET /blogs/:id/previous(.:format) blogs#previous 
      user_root GET /blog/root(.:format)    blogs#root 

어떤 단서?

감사합니다.

+0

사용중인 '보기 코드'를 게시하십시오. – Pavan

답변

4

edit_blog_path에는 블로그가 인수로 필요합니다.

edit_blog_path(@blog) 예를 들면.

블로그를 편집하지 않고도 edit_blog 페이지에 있는지 만 확인하려는 경우 다음과 같이 할 수 있습니다.

if controller_name == 'blogs' && action_name == 'edit' 

방금 ​​새로운 편집 형태 사이의 구별하려는 경우에도 당신은 할 필요

if @blog.persisted? 
    # existing blog 
+0

왜 이것을 downvoted입니까? 대답은 정확하지만 다른 답변만큼 철저하지 않을 수도 있습니다. – Arjan

+0

'id' 만 전달할 수있는 완전한 대답이 아닙니다. – Migol

+0

아직 완료되지 않았지만 잘못되지 않았으므로, 여기에 downvote가 없을 것입니다. – Arjan

1

중 하나를 통과 할 수 id

if current_page?(edit_blog_path(id: @blog.id)) 

또는 같은 전체 블로그 개체를 전달

같은

if current_page?(edit_blog_path(@blog)) 
+0

특히, 이미'@ blog'를 메모리에 가지고 있는데, 객체 자체를 전달할 수있을 때 왜 이드를 넘겨 줄까요? 라인이 길어질수록 호기심이 생기고 PIE는 적습니다. '@ blog'을 편집하고 있습니다. 물론 작동하지만, 이런 식으로 사용하는 특별한 유스 케이스는 생각할 수 없습니다. 또한 위의 대답은 질문에 대답 할 때 완료됩니다. – Sean

0

경로 도우미 edit_blog_path에 ID가 필요하다고 말하고 싶습니다. 아마도 edit_blog_path (@blog)가 작동 할 수도 있고 @blog를 거기에있는 변수로 바꿀 수도 있습니까?

+0

'id'만 전달할 수있는 완전한 대답이 아닙니다. – Migol

관련 문제