语法错误,我的应用程序中出现意外的keyword_ensure,期望keyword_end

时间:2019-05-10 12:01:30

标签: ruby-on-rails ruby

请我需要帮助验证此代码。 使用Ruby 2.3.3和Rails 5.2 我认为所有结尾锚都已给出


<% if current_user.id != user.id %>
<div class="panel panel-default">
    <div class="pane-body">
        <center>
            <% if !current_user.following?(user) %>
            <%= form_for(current_user.active_relationships.build) do |f| %>
            <div><%= hidden_field_tag :followed_id, user.id %></div>
            <%= f.submit "Follow", class: "btn btn-primary" %>
            <% end %>
        <% else %>
             <%= form_for(current_user.active_relationships.find_by(followed_id: user.id),
              html: {method: :delete}) do |f| %>
              <%= f.submit "Unfollow", class: "btn btn-default" %>
             <% end %>
        </center>

    </div>

</div>
<% end %>

2 个答案:

答案 0 :(得分:2)

这里

  <% else %>
    <%= form_for(current_user.active_relationships.find_by(followed_id: user.id), html: {method: :delete}) do |f| %>
      <%= f.submit "Unfollow", class: "btn btn-default" %>
    <% end %>
</center>

您有end可以关闭form_for的代码段,但不能结束if-else

  <% else %>
    <%= form_for(current_user.active_relationships.find_by(followed_id: user.id), html: {method: :delete}) do |f| %>
      <%= f.submit "Unfollow", class: "btn btn-default" %>
    <% end %>
  <% end %>
</center>

答案 1 :(得分:2)

正确缩进有很大帮助:

<% if current_user.id != user.id %>
  <div class="panel panel-default">
    <div class="pane-body">
      <center>
        <% if !current_user.following?(user) %>
          <%= form_for(current_user.active_relationships.build) do |f| %>
            <div><%= hidden_field_tag :followed_id, user.id %></div>
            <%= f.submit "Follow", class: "btn btn-primary" %>
          <% end %>
        <% else %>
          <%= form_for(current_user.active_relationships.find_by(followed_id: user.id), html: {method: :delete}) do |f| %>
            <%= f.submit "Unfollow", class: "btn btn-default" %>
          <% end %>
        <% end %>
      </center>
    </div>
  </div>
<% end %>

第13行的端点关闭了form_for do |f|块,这意味着您还需要一个端点来封闭if \ else do