目前,我在为一个用户显示“关注/取消关注”按钮时拥有完整的功能。 我可以正常关注和取消关注任何用户。
但是,显示所有用户的列表并在每个用户上显示“关注/取消关注”按钮时。
当我点击关注时,我得到了。
我为#<#:0x007fa8a9b4f448> 获取未定义的局部变量或方法`follow' 在_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
答案 0 :(得分:0)
尝试将_show_follow.html.erb更改为:
<% @users.each do |following|%>
<div class="span1">
<% f = signed_in? ? following : ''
<%= render 'follow_aud_form', :following => f %>
</div>
<% end %>
我相信你遇到的唯一问题是除非用户登录,否则不会创建本地var :following
。建议的更改只是确保无论如何都要创建局部变量。