1

这是我的 show.html.erb 文件。

<% provide(:title, @user.name) %>
<div class="row">
    <aside class="span4">
        <section>
            <h1>
                <%= gravatar_for @user %>
                <%= @user.name %>
            </h1>
        </section>
        <section>
            <%= render 'shared/stats' %>
        </section>
    </aside>
    <div class="span8">
        <%= render 'follow_form' if signed_in? %>
    <% if @user.microposts.any? %>
      <h3>Microposts (<%= @user.microposts.count %>)</h3>
      <ol class="microposts">
        <%= render @microposts %>
      </ol>
      <%= will_paginate @microposts %>
    <% end %>
  </div>
</div>

_follow.html.erb 部分文件:

<%= form_for(current_user.relationships.build(followed_id: @user.id),
             remote: true) do |f| %>
  <div><%= f.hidden_field :followed_id %></div>
  <%= f.submit "Follow", class: "btn btn-large btn-primary" %>
<% end %>

_unfollow.html.erb 部分文件:

<%= form_for(current_user.relationships.find_by_followed_id(@user),
             html: { method: :delete },
             remote: true) do |f| %>
  <%= f.submit "Unfollow", class: "btn btn-large" %>
<% end %>

而我的 _follow_form.html.erb 部分:

<% unless current_user?(@user) %>
  <div id="follow_form">
  <% if current_user.following?(@user) %>
    <%= render 'unfollow' %>
  <% else %>
    <%= render 'follow' %>
  <% end %>
  </div>
<% end %>

编辑:我还想说我正在使用 Bootstrap。当我检查用户页面时,两个类 span4 和 span8 都出现了,但是“follow_form”应该是一个完整的空白。

编辑:这个问题是几年前回答的:)

4

1 回答 1

0

我遇到了同样的问题,您可以通过以下更改来纠正此问题:

步骤 1)在 app/views/users/show.html.erb 中更改代码:

<div class = "span8">
    <%= render 'follow_form' if signed_in? %>      
        <% if @user.microposts.any? %>

对此:

<div class = "span8">
    <%= render 'shared/follow_form' if signed_in? %>      
        <% if @user.microposts.any? %>

步骤 2) 在部分 app/views/shared/_follow_form.html.erb

从此更改代码:

<% unless current_user?(@user) %>
  <div id="follow_form">
  <%if current_user.following?(@user) %>
    <%= render 'unfollow' %>
  <% else %>
    <%= render 'follow' %>
  <% end %>
  </div>
<% end %>

对此:

<% unless current_user?(@user) %>
  <div id="follow_form">
  <%if current_user.following?(@user) %>
    <%= render 'shared/unfollow' %>
  <% else %>
    <%= render 'shared/follow' %>
  <% end %>
  </div>
<% end %>
于 2012-08-11T07:08:43.453 回答