我正在阅读micheal hart ruby on rails tutrial,我在用户显示页面上有关注和取消关注按钮,但后来我尝试将其添加到用户索引页面索引页面我收到此错误
undefined method `id' for nil:NilClass
这是表单
的表单代码<% user ||= current_user %>
<div id="follow_form">
<% if current_user.following? user %>
<%= form_for(current_user.active_relationships.find_by(followed_id: @user.id),html: { method: :delete },remote: true) do |f| %>
<%= f.submit "Unfollow", class: "waves-effect waves-light btn" %>
<% end %>
<% else %>
<%= form_for(current_user.active_relationships.build(followed_id: @user.id), remote: true) do |f| %>
<div><%= hidden_field_tag :followed_id, @user.id %></div>
<%= f.submit "Follow", class: "waves-effect waves-light btn" %>
<% end %>
<% end %>
添加后我仍然得到相同的错误。表单使用此<%= render @users %>
进行渲染,部分包含此`
</div>
<div class="user-profile-inner">
<h6 >
<%= link_to user.name, user %></h4>
<%= gravatar_all user %>
<h5><%= user.position %></h5>
<!-- User button -->
<div class="user-button">
<div class="row">
<% if current_user.admin? && !current_user?(user) %>
<div class="col m6">
<%= link_to "delete", user, method: :delete,class: 'waves-effect waves-light btn red darken-4',
数据:{确认:&#34;你确定吗?&#34; }%&gt;
<% end %>
<div class="col m6">
<button type="button" class="btn btn-default btn-sm btn-block"><i class="fa fa-user"></i> <%= render "follow_form_user" %> </button>
</div>
<div class="col m6">
<% unless current_user == @user %>
<%= link_to 'Send message', new_message_path(to: user.id), class: 'waves-effect waves-light btn' %>
<% end %>
</div>
</div>
</div>
</div>
</div>
`
我需要在其上添加代码吗?
答案 0 :(得分:0)
我认为你这里没有@user
个实例变量,只有user
。在此处将@user
替换为user
。