Kel*_*lly 8 erb ruby-on-rails-4
我在html.erb中遇到了if,elsif,else语句的问题.我在erb中看到了很多关于if/else语句的问题但没有包含elsif的问题,所以我想我会请求帮助.
这是我的html.erb:
<% if logged_in? %>
<ul class = "nav navbar-nav pull-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu pull-right">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", edit_user_path(current_user) %></li>
<li class="divider"></li>
<li>
<%= link_to "Log out", logout_path, method: "delete" %>
</li>
</ul>
</li>
</ul>
<% elsif has_booth?(current_user.id) %>
<ul>
<li>TEST</li>
</ul>
<% else %>
<ul class="nav navbar-nav pull-right">
<li><%= link_to "Sign Up", signup_path %></li>
<li><%= link_to "Log in", login_path %></li>
</ul>
<% end %>
Run Code Online (Sandbox Code Playgroud)
这是我的has_booths方法:
module BoothsHelper
def has_booth?(user_id)
Booth.exists?(user_id: user_id)
end
end
Run Code Online (Sandbox Code Playgroud)
我希望标题导航为不同的用户提供三种不同类型的内容.登录用户,已创建booth的登录用户以及已注销用户.到目前为止,我似乎只能在三项工作中做出2项.我试过改变
<% elsif has_booth?(current_user.id) %>
Run Code Online (Sandbox Code Playgroud)
至
<% elsif logged_in? && has_booth?(current_user.id) %>
Run Code Online (Sandbox Code Playgroud)
那也不起作用.我正确地写了我的陈述吗?任何想法都赞赏.谢谢.
Ric*_*dAE 19
问题是你的第一个条件是真的,所以它停在那里.你的第一个条件:
<% if logged_in? %>
Run Code Online (Sandbox Code Playgroud)
即使他们没有展位也永远不会到达elsif,因为第一个条件是真的.你要么需要:
<% if logged_in? && has_booth?(current_user.id) %>
// code
<% elsif logged_in? && !has_booth?(current_user.id) %>
// code
<% else %>
// code
<% end %>
Run Code Online (Sandbox Code Playgroud)
或者它可能是一个更简洁的方法将它们分成两个if/else:
<% if logged_in? %>
<% if has_booth?(current_user.id) %>
// code
<% else %>
// code
<% end %>
<% else %>
// code
<% end %>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
35801 次 |
| 最近记录: |