我是ROR的新手。我试图用条件进行切换。有人可以帮我正确的代码
<%= link_to "Profile", (user = User.find(1)
case user.role
when "A"
redirect_to(url)
when "B"
redirect_to(url)
when "C"
redirect_to(url)
else
redirect_to(url)
end)%></i>
答案 0 :(得分:0)
尝试使用以下代码。
<% user = User.find(1) %>
<% case user.role %>
<% when "A" %>
<%= link_to "Profile", "YOUR URL" %>
<% when "B" %>
<%= link_to "Profile", "YOUR URL" %>
<% when "C" %>
<%= link_to "Profile", "YOUR URL" %>
<% else %>
<%= link_to "Profile", "YOUR URL" %>
<% end %>
将此代码写入视图文件并不好。 rails提供帮助方法来为视图文件编写业务逻辑。
答案 1 :(得分:0)
在用户帮助
中 def role_link(user)
case user.role
when 'A'
url = 'redirect_url'
when 'B'
url = 'redirect_url'
when 'C'
url = 'redirect_url'
else
url = 'redirect_url'
end
url
end
在视图文件
中 <%= link_to 'profile', role_link(User.find(1) %>