有条件的 erb 没有按预期工作
conditional erb not working as expected
我有一个下拉菜单,其中 link 指向用户的个人资料页面 (UserProfile
)。 user.user_profile
不是默认创建的,因此 link 应该只在关联的用户配置文件存在时显示。
我目前有:
<% 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'
`
更新:
rake 路线:
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
这可能会解决您的问题
如果您在用户 table 中有一个列 user_profile 并且它的 data_type 是布尔值,那么您可以使用:
if current_user.user_profile?
直接没有必要在helper中创建一个单独的方法。
如果没有配置文件存在 user_profile false 的设置值,并且当用户创建配置文件时将 user_profile 设置为 1 或 true。
在进行修复的同时,建议从您的视图中删除条件并放入辅助方法中。
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
雇员再培训局:
<%= user_profile_link %>
我有一个下拉菜单,其中 link 指向用户的个人资料页面 (UserProfile
)。 user.user_profile
不是默认创建的,因此 link 应该只在关联的用户配置文件存在时显示。
我目前有:
<% 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'
` 更新:
rake 路线:
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
这可能会解决您的问题
如果您在用户 table 中有一个列 user_profile 并且它的 data_type 是布尔值,那么您可以使用:
if current_user.user_profile?
直接没有必要在helper中创建一个单独的方法。 如果没有配置文件存在 user_profile false 的设置值,并且当用户创建配置文件时将 user_profile 设置为 1 或 true。
在进行修复的同时,建议从您的视图中删除条件并放入辅助方法中。
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
雇员再培训局:
<%= user_profile_link %>