RelationshipsController#destroy中的ActiveRecord :: RecordNotFound

时间:2015-12-26 04:50:28

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

您好我正在尝试构建一个类似于应用程序的推文。我创建了一个关系模型。在我的视图中,我有两个表单,一个带有跟随按钮,另一个带有取消关注按钮,在我的视图中看起来像。

<% 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

1 个答案:

答案 0 :(得分:0)

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

#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 %>

提交过程取决于将请求作为url.com/users/5/followPOST发送至DELETE

在控制器级别,您可以添加或删除以下关系(followers将是其他人正在做的事情)。这样,每次要创建新订阅时,都不需要为form_for设置对象。

您的表格可以与上述代码保持一致。