目前,我在为一个用户显示关注/取消关注按钮时拥有全部功能。 我能够正确地关注和取消关注任何用户。
但是,当显示所有用户的列表并在每个用户上显示关注/取消关注按钮时。
当我点击关注时,我得到了。
我得到undefined local variable or method `following' for #<#:0x007fa8a9b4f448> 在 _unfollow.html.erb 的第二行 - 完整错误请参见下面的照片
我在渲染局部时传入变量。
<%= render 'follow_aud_form', :following => following if signed_in? %>
我还能够检查“following”并查看实例数据。
点击后返回时,它实际上是关注/取消关注所选用户,但是它首先产生错误
见下面的代码
用户 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 %>
关注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
最佳答案
只需尝试将 _show_follow.html.erb 更改为:
<% @users.each do |following|%>
<div class="span1">
<% f = signed_in? ? following : ''
<%= render 'follow_aud_form', :following => f %>
</div>
<% end %>
我相信您遇到的唯一问题是,除非用户已登录,否则不会创建本地变量 :following
。提议的更改只是确保无论如何都会创建本地变量。
关于ruby-on-rails - 遵循对象所有实例的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18131144/