2016-08-04 4 views
0

사용자 프로필 페이지 (UserProfile)에 대한 링크가있는 드롭 다운 메뉴가 있습니다. user.user_profile은 기본적으로 만들어지지 않으므로 연결된 사용자 프로필이있는 경우에만 링크가 표시됩니다.조건부 erb가 예상대로 작동하지 않습니다.

내가 현재 가지고 :

<% if profile_present? %> 
<%= link_to "My profile", user_profile_path(current_user.user_profile) %> 
<% end %> 

내 도우미 방법 :

module ApplicationHelper 
    def profile_present? 
    current_user.user_profile.present? if user_signed_in? 
    end 
end 

목표는 조건이 충족되는 경우에만 코드를 실행하는 것입니다.

제안 사항?

사용자 프로필이 없으면이 메시지는 ActionController::UrlGenerationError입니다.

No route matches {:action=>"show", :controller=>"user_profiles", :id=>nil} missing required keys: [:id] 

응용 프로그램 추적 :

app/views/layouts/_navbar.html.erb:44:in `_app_views_layouts__navbar_html_erb___2344119771953232299_47030678815860' 
app/views/layouts/application.html.erb:27:in `_app_views_layouts_application_html_erb___1177681419623347300_69843401266740' 

` UPDATE :

레이크 경로 :

    user_profiles GET  /user_profiles(.:format)    user_profiles#index 
           POST  /user_profiles(.:format)    user_profiles#create 
       new_user_profile GET  /user_profiles/new(.:format)   user_profiles#new 
       edit_user_profile GET  /user_profiles/:id/edit(.:format)  user_profiles#edit 
        user_profile GET  /user_profiles/:id(.:format)   user_profiles#show 
           PATCH /user_profiles/:id(.:format)   user_profiles#update 
           PUT  /user_profiles/:id(.:format)   user_profiles#update 
           DELETE /user_profiles/:id(.:format)   user_profiles#destroy 
+0

이 간단한 것이 될 수도 있지만, 'profile_present? '메소드를 게시하지 않으면 알기가 어렵습니다. – MarsAtomic

+0

이 오류는 아마도'profile_present? '조건에있을 것입니다. 오류에 대한 자세한 정보를 제공해 주시겠습니까? – Phil

+0

현재 사용자를 얻는 것뿐만 아니라'profile_present'에 대한 코드를 공유 할 수 있습니까? 확률은'nil'을'user_profile_path'에 전달하고 오류를 던지고 있습니다. – nicholas79171

답변

1

, 그것은 ... 당신의보기에서와 도우미 메서드에 조건을 제거하는 것이 좋습니다

Module ApplicationHelper 
    def user_profile_link 
    if current_user.user_profile? 
     link_to 'My profile', user_profile_path(current_user.user_profile) 
    end 
    end 
end 

ERB :

<%= user_profile_link %> 
+0

감사합니다. 불행히도 이것은 여전히 ​​나에게 같은 오류를 준다 .... – Matthias

+0

업데이트 된 게시물 전체 오류 메시지보기 – Matthias

+0

@Matthias가 내 대답을 업데이트했습니다. 경로 도우미에 올바른 인수를 전달하지 않았기 때문일 수 있습니다. '레이크 루트 '의 결과는 무엇입니까? '... path (current_user)'또는'... path (current_user, current_user.user_profile) '이어야합니다. –

0

이 문제를 해결할 수 있습니다

당신은 사용자 테이블에서 열 user_profile로가 그 DATA_TYPE 경우 부울는 다음 사용할 수 있습니다

if current_user.user_profile? 

직접 도우미에 별도의 방법을 만들 필요가 없다. 프로필이없는 경우 user_profile의 값을 false로 설정하고 사용자가 프로필을 만들 때 user_profile을 1 또는 true로 설정합니다. 수정과 함께

관련 문제