【问题标题】:Follow actions for all instances of object遵循对象所有实例的操作
【发布时间】:2013-08-10 11:14:02
【问题描述】:

目前,在为一位用户显示关注/取消关注按钮时,我拥有完整的功能。 我能够正确关注和取消关注任何用户。

但是,当显示所有用户的列表并在每个用户上显示关注/取消关注按钮时。

当我点击关注时,我得到了。

我得到未定义的局部变量或方法“跟随”# 在 _unfollow.html.erb 的第二行 - 完整错误请见下图

我在渲染部分时传入了变量。

<%= render 'follow_aud_form', :following => following if signed_in? %> 

我还可以检查“关注”并查看实例数据。

当我返回点击后,它实际上跟随/取消关注选定的用户,但是它首先产生错误

见下方代码

users_controller

def following
 @title = "Following"
 @user = User.find(params[:id])
 @users = @user.followed_users
 render 'show_follow'
end

def followers
  @title = "Followers"
  @user = User.find(params[:id])
  @users = @user.followers
  render 'show_follow'
end

show_follow.html.erb

<% @users.each do |following|%>

        <div class="span1">
          <%= render 'follow_aud_form', :following => following if signed_in? %>
        </div>

    <% end %>

follow_aud_form

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

_follow_html

|__<%= @gaza_id = following.id %>__|
<%= form_for(current_user.relationships.build(followed_id: @gaza_id)) do |f| %>
  <div><%= f.hidden_field :followed_id %></div>
  <%= f.submit "Follow",:type => :image, :src => "/assets/follow.png" %>
<% end %>

_unfollow.html

|__<%= @slim_id = following.id %>__|
<%= form_for(current_user.relationships.find_by_followed_id(@slim_id),
             html: { method: :delete }) do |f| %>
  <%#= f.submit "Unfollow", class: "btn btn-large" %>
  <%= f.submit "Unfollow",:type => :image, :src => "/assets/following.png" %>
<% end %>

我也看过Users list with follow/unfollow button

【问题讨论】:

  • 请检查(并告诉我们)出现错误的行。似乎应该在您正在执行 some_obj.following 的某个地方(可能是以下? User 类中的方法)。请提供此方法中的逻辑,以便我检查。
  • 当我没有在渲染调用中明确使用template 选项时,我自己经常遇到问题,可能值得指定完整路径并同时使用locals 块。例如,尝试将&lt;%= render 'follow_aud_form', :following =&gt; following if signed_in? %&gt; 更改为&lt;%= render :template =&gt; 'some_controller/follow_aud_form', :locals =&gt; {:following =&gt; following} if signed_in? %&gt;
  • @RafaelMartinez 添加行号和照片
  • @Noz 同样的事情,当我尝试 'users/_follow_aud_form', :locals => {:following => following} if signed_in? %> 也没有 :locals

标签: ruby-on-rails ruby relationship twitter-follow


【解决方案1】:

只需尝试将 _show_follow.html.erb 更改为:

<% @users.each do |following|%>

    <div class="span1">
      <% f = signed_in? ? following : ''
      <%= render 'follow_aud_form', :following => f %>
    </div>

<% end %>

我相信您遇到的唯一问题是除非您的用户已登录,否则不会创建本地变量 :following。提议的更改只是确保无论如何都会创建本地变量。

【讨论】:

  • 如果您不想显示“follow_aud_form”,除非用户已登录,您应该使用括号: following) if signed_in? %>
猜你喜欢
  • 2010-11-17
  • 1970-01-01
  • 2010-09-28
  • 2016-10-25
  • 2017-06-09
  • 2022-01-13
  • 1970-01-01
  • 2017-09-23
  • 2014-12-07
相关资源
最近更新 更多