Link_to_if - 文本仍然存在
Link_to_if - text still present
我想做一个有条件的 link_to
,只有当用户以管理员身份登录时才会显示。所以我这样做了:
应用程序控制器
def confirm_admin
if session[:role] == "admin"
@isAdmin = true
else
flash[:notice] = "Vous n'avez pas les droits pour accéder à cette page."
redirect_to root_path
return false
end
end
查看
<%= link_to_if is_admin, 'Editer l\'idée', edit_idee_path(@idee), :class => "editer custom-button" %>
link 消失了,但文字仍然存在,您知道为什么吗?
link_to_if
仅使文本无-link 文本;它从文本中删除 link,而不是文本本身。
你想要做什么,你可以通过下面的代码实现它:
<% if is_admin %>
<%= link_to 'Editer l\'idée', edit_idee_path(@idee), :class => "editer custom-button" %>
<% end %>
<%# Now, it will remove both: link as well as text. %>
更好的方法是在link_to_if语句的末尾添加一个空块,这将在条件为真时显示文本,而在条件为假时不显示。
例如
flash[:notice]= "Some text that will always show.
#{view_context.link_to_if(user.exists?,
'A hyperlink that will show based on conditional',
user_path)**{}**}"
我想做一个有条件的 link_to
,只有当用户以管理员身份登录时才会显示。所以我这样做了:
应用程序控制器
def confirm_admin
if session[:role] == "admin"
@isAdmin = true
else
flash[:notice] = "Vous n'avez pas les droits pour accéder à cette page."
redirect_to root_path
return false
end
end
查看
<%= link_to_if is_admin, 'Editer l\'idée', edit_idee_path(@idee), :class => "editer custom-button" %>
link 消失了,但文字仍然存在,您知道为什么吗?
link_to_if
仅使文本无-link 文本;它从文本中删除 link,而不是文本本身。
你想要做什么,你可以通过下面的代码实现它:
<% if is_admin %>
<%= link_to 'Editer l\'idée', edit_idee_path(@idee), :class => "editer custom-button" %>
<% end %>
<%# Now, it will remove both: link as well as text. %>
更好的方法是在link_to_if语句的末尾添加一个空块,这将在条件为真时显示文本,而在条件为假时不显示。
例如
flash[:notice]= "Some text that will always show.
#{view_context.link_to_if(user.exists?,
'A hyperlink that will show based on conditional',
user_path)**{}**}"