【问题标题】:ActiveRecord::RecordNotFound in RelationshipsController#destroy关系控制器中的 ActiveRecord::RecordNotFound#destroy
【发布时间】:2015-12-26 04:50:27
【问题描述】:

您好,我正在尝试构建一个类似 twitter 的示例应用程序。我创建了一个关系模型,在我看来,我有两种形式,一种带有关注按钮,另一种带有取消关注按钮,看起来像这样。

<% if current_user.following?(@otheruser) %>
     <%= render 'unfollow' %>
<% else %>
     <%= render 'follow' %>
 <% end %>

我在 _follow.html.erb 中喜欢

<%= form_for(current_user.active_relationships.build) do |f| %>
  <div><%= hidden_field_tag :followed_id, @otheruser.id %></div>
  <%= f.submit "Follow", class: "btn btn-primary" %>
<% end %>

在 _unfollow.html.erb 之类的

<%= form_for(current_user.active_relationships.find_by(followed_id: @otheruser.id),
             html: { method: :delete }) do |f| %>
  <%= f.submit "Unfollow", class: "btn btn-danger" %>
<% end %>

在我的 user.rb 中 我有像

这样的方法和关联
has_many :active_relationships,  class_name:  "Relationship",
         foreign_key: "follower_id",
         dependent:   :destroy
has_many :passive_relationships, class_name:  "Relationship",
         foreign_key: "followed_id",
         dependent:   :destroy
has_many :following, through: :active_relationships,  source: :followed
has_many :followers, through: :passive_relationships, source: :follower   

  def follow(other_user)
    active_relationships.create(followed_id: other_user.id)
  end

# 取消关注用户。

def unfollow(other_user)
   active_relationships.find_by(followed_id: other_user.id).destroy
end

每当我点击关注按钮时,它都会在relationship.rb中的关系模型中创建一个条目

class Relationship < ActiveRecord::Base
  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "User"
  validates :follower_id, presence: true
  validates :followed_id, presence: true
end

但是当我点击取消关注按钮时,它给出的错误是“找不到与'id'的关系=” 任何人都可以帮助我了解如何显示 user.followers 和 user.following 的东西。

在关系控制器中就像

 class RelationshipsController < ApplicationController
  def index
    @otheruser =  User.find_by_username(params[:name])
    @reviews  =  @otheruser.write_reviews.all
    @followers  = @otheruser.followers
  end
  def create
    user = User.find(params[:followed_id])
    current_user.follow(user)
    redirect_to followuser_url
  end
  def destroy
    user = Relationship.find(params[:id]).followed
    current_user.unfollow(user)
    redirect_to followuser_url
  end
end

关系表看起来像

create_table "relationships", force: :cascade do |t|
    t.integer  "follower_id", limit: 4
    t.integer  "followed_id", limit: 4
    t.datetime "created_at",            null: false
    t.datetime "updated_at",            null: false
  end

【问题讨论】:

  • 让我们看看你的控制器
  • 我已经更新了我的问题,请参考。
  • 点击取消关注btn时调用哪个方法
  • 关系#destroy
  • 请任何人帮忙...

标签: ruby-on-rails ruby ruby-on-rails-3 activerecord


【解决方案1】:

如果需要,我会删除它,我无法忍受你的代码到处都是:

#config/routes.rb
resources :users do
   resources :relationships, path: "follow", only: [:create, :destroy]
end

#app/controllers/relationships_controller.rb
class RelationshipsController < ApplicationController
   before_action :set_user

   def create
      current_user.following << user
   end

   def destroy
      current_user.following.destroy(user)
   end

   private

   def set_user
       @user = User.find follow_params
   end

   def follow_params
      params.permit(:user_id)
   end
end

#app/models/user.rb
class User < ActiveRecord::Base
    has_many :followers, class_name: "Relationship" #-> users following you
    has_many :following, class_name: "Relationship", foreign_key: :follower_id, association_foreign_key: :user_id #-> users you are following
end

#app/models/relationship.rb
class Relationship < ActiveRecord::Base
    belongs_to :user
    belongs_to :follower, class_name: "User"

    validates :user, :follower, presence: true
    validates :user_id, uniqueness: { scope: :follower_id }
end

--

这将允许您使用以下内容:

#app/views/relationships/_follow.html.erb
<% message = current_user.following?(user) ? "Unfollow" : "Follow" %>
<% method = current_user.following?(user) ? "method: :delete" : "" %>
<%= button_to message, user_relationship_path(user), eval(method), class: "btn btn-primary" %>

#app/views/users/index.html.erb
<% @users.each do |user| %>
   <%= render "relationships/follow", locals: { user: user } %>
<% end %>

提交过程取决于将请求以POSTDELETE 发送到url.com/users/5/follow

在控制器级别,您将能够添加或删除 following 关系(followers 将是其他人所做的)。这样,您无需在每次要创建新订阅时都为 form_for 设置对象。


您的表格可以与上面的代码保持不变。

【讨论】:

  • 您可以直接格式化问题中的代码,而不是再次粘贴。
  • 是的,我没有粘贴它。我将其全部更改为更有效,这就是为什么我提到如果合适的话我会删除答案。我觉得你可以做一个更简单的实现
  • 你的意思是我不需要 _unfollow.html.erb 而是我可以在 _follow.html.erb 中渲染,而不是 follow_id 我应该使用 user_id 。
  • 你能在 index.html.erb 中解释一下为什么你使用@user.each 吗
  • 不,我的意思是你可以去掉一半的 form_for 引用和 passive_relationship 等。
猜你喜欢
  • 2015-12-21
  • 1970-01-01
  • 2019-11-25
  • 2017-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多