Ruby on rails 未定义的方法`find#u by';

Ruby on rails 未定义的方法`find#u by';,ruby-on-rails,ruby,Ruby On Rails,Ruby,我是RubyonRails新手,所以请帮助我:)这是我的错误 NoMethodError in Welcome#sucess Showing ..app/views/welcome/sucess.html.erb where line #8 raised: undefined method `find_by' for #<ActiveRecord::Relation:0x41dca80> Extracted source (around line #8): 5: &l

我是RubyonRails新手,所以请帮助我:)这是我的错误

NoMethodError in Welcome#sucess   
Showing ..app/views/welcome/sucess.html.erb where line #8 raised:    
undefined method `find_by' for #<ActiveRecord::Relation:0x41dca80>
Extracted source (around line #8):

5: <%= @user.pass%><br />
6: <% if current_user && current_user != @user %>
7:   <% if current_user.following.include?(@user) %>
8:     <%= form_for(current_user.following_relations.find_by(following_id: @user.id),
9:              html: { method: :delete }) do |f| %>
10:   <%= f.submit "Unfollow", class: "btn" %>
11: <% end %>
我的用户模型

  def unfollow(other_user)
    following_relations.find_by(following_id: other_user.id).destroy
  end
请帮助我解决此问题

您可以尝试以下方法:

<%= form_for(current_user.following_relations.find_by_following_id(@user.id),html: { method: :delete }) do |f| %>
希望它能工作

您可以尝试以下方法:

<%= form_for(current_user.following_relations.find_by_following_id(@user.id),html: { method: :delete }) do |f| %>
希望它能工作

您可以尝试以下方法:

<%= form_for(current_user.following_relations.find_by_following_id(@user.id),html: { method: :delete }) do |f| %>
希望它能工作

您可以尝试以下方法:

<%= form_for(current_user.following_relations.find_by_following_id(@user.id),html: { method: :delete }) do |f| %>

希望它能工作

因为您使用的Rails版本(3.2.19)不支持
find_by()
,所以您需要使用旧版本

因此,如果您改变,它很可能会起作用:

def unfollow(other_user)
  following_relations.find_by(following_id: other_user.id).destroy
end


由于您使用的Rails版本(3.2.19)不支持
find_by()
,因此需要使用旧版本

因此,如果您改变,它很可能会起作用:

def unfollow(other_user)
  following_relations.find_by(following_id: other_user.id).destroy
end


由于您使用的Rails版本(3.2.19)不支持
find_by()
,因此需要使用旧版本

因此,如果您改变,它很可能会起作用:

def unfollow(other_user)
  following_relations.find_by(following_id: other_user.id).destroy
end


由于您使用的Rails版本(3.2.19)不支持
find_by()
,因此需要使用旧版本

因此,如果您改变,它很可能会起作用:

def unfollow(other_user)
  following_relations.find_by(following_id: other_user.id).destroy
end



您使用的是哪个版本的Rails?find_by仅适用于rails>=4.0我使用的是rails版本3.2.19您使用的是哪一版本的rails?find_by仅适用于rails>=4.0我使用的是rails版本3.2.19您使用的是哪一版本的rails?find_by仅适用于rails>=4.0我使用的是rails版本3.2.19您使用的是哪一版本的rails?find_by works only on rails>=4.0我正在使用rails版本3.2.19解释为什么这项工作会很棒,因为这是公认的答案。@sevensacat Well,Rails
find_by
正在查找最具体的条件。因此,如果您在
find_by
上指定属性条件,则可以指定8:
@user.id)
,这完全不是您的答案正确的原因。请参见下面罗宾的回答。@Sevensacat抱歉,我接受罗宾的解释对我的回答是正确的。鉴于这是公认的答案,我解释了为什么这项工作会很棒。@Sevensacat很好,Rails
find_by
正在查找最具体的条件。因此,如果您在
find_by
上指定属性条件,则可以指定8:
@user.id)
,这完全不是您的答案正确的原因。请参见下面罗宾的回答。@Sevensacat抱歉,我接受罗宾的解释对我的回答是正确的。鉴于这是公认的答案,我解释了为什么这项工作会很棒。@Sevensacat很好,Rails
find_by
正在查找最具体的条件。因此,如果您在
find_by
上指定属性条件,则可以指定8:
@user.id)
,这完全不是您的答案正确的原因。请参见下面罗宾的回答。@Sevensacat抱歉,我接受罗宾的解释对我的回答是正确的。鉴于这是公认的答案,我解释了为什么这项工作会很棒。@Sevensacat很好,Rails
find_by
正在查找最具体的条件。因此,如果您在
find_by
上指定属性条件,则可以指定8:
@user.id)
,这完全不是您的答案正确的原因。见下面罗宾的回答。@Sevensacat抱歉,我接受罗宾的解释对我的回答是正确的