我知道之前已经回答了这个问题,但是我已经阅读了大部分内容,但我仍然无法弄明白。有人知道会发生什么事吗?
<% if current_user.try(:email) == Join.all(:email) %>
<div class="panel-default">
<div class="panel-body">
You're already signed up for this month! Thanks!
</div>
</div>
<% else %>
<% if user_signed_in? %>
<%= form_for @join do |f| %>
<%= f.email_field :email, value: current_user.email, class: "form-control input-lg", id: "join" %> <br>
<%= f.submit "Join This Month's Rewards", class: "btn btn-success btn-lg" %>
<% end %>
<% end %>
<% else %>
<div class="panel panel-default">
<div class="panel-body">
Sorry, you're not currently signed in, so you can't sign up for our rewards. You can sign in <a href="/users/sign_in">here.</a>
</div>
</div>
<% end %>
以下是完整错误:/Users/Sam/Documents/workspace/rewardsApp/app/views/joins/new.html.erb:19: syntax error, unexpected keyword_else, expecting keyword_end else ^
错误位于第29行,即<% else %>
之后的<% end %>
非常感谢帮助!
答案 0 :(得分:0)
您的if
/ else
设置似乎过早结束,以便考虑视图的三种状态。
<% if current_user.try(:email) == Join.all(:email) %>
<div class="panel-default">
<div class="panel-body">
You're already signed up for this month! Thanks!
</div>
</div>
<% else %>
<% if user_signed_in? %>
<%= form_for @join do |f| %>
<%= f.email_field :email, value: current_user.email, class: "form-control input-lg", id: "join" %> <br>
<%= f.submit "Join This Month's Rewards", class: "btn btn-success btn-lg" %>
<% else %>
<div class="panel panel-default">
<div class="panel-body">
Sorry, you're not currently signed in, so you can't sign up for our rewards. You can sign in <a href="/users/sign_in">here.</a>
</div>
</div>
<% end %>
<% end %>
答案 1 :(得分:0)
这可能是为什么适当地缩进代码非常有用的一个很好的例子。一旦这样做,很明显你有第二个else
条款。
<% if current_user.try(:email) == Join.all(:email) %>
<div class="panel-default">
<div class="panel-body">
You're already signed up for this month! Thanks!
</div>
</div>
<% else %>
<% if user_signed_in? %>
<%= form_for @join do |f| %>
<%= f.email_field :email, value: current_user.email, class: "form-control input-lg", id: "join" %> <br>
<%= f.submit "Join This Month's Rewards", class: "btn btn-success btn-lg" %>
<% end %>
<% end %>
<% else %> <%# <= Uh oh, second else clause %>
<div class="panel panel-default">
<div class="panel-body">
Sorry, you're not currently signed in, so you can't sign up for our rewards. You can sign in <a href="/users/sign_in">here.</a>
</div>
</div>
<% end %>