ユーザーのプロフィールページ(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
これは単純なものかもしれませんが、あなたが 'profile_present? 'メソッドを投稿しない限り、それを伝えるのは難しいです。 – MarsAtomic
このエラーはおそらく 'profile_present? 'の状態にあります。エラーについての詳細情報も提供できますか? – Phil
'profile_present'のコードを共有するだけでなく、現在のユーザーを取得できますか?これは、 'user_profile_path'に' nil'を渡してエラーを投げていることです。 – nicholas79171