RelationshipsController中的ActiveRecord :: RecordNotFound #create

时间:2015-12-27 11:00:25

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

我想创建像追随者和追随者的推特。 在我看来,我有

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

中的

 <%= form_for (@otheruser), url: createfollower_path(@otheruser) ,:class=>"form-horizontal",method: :post do |f|  %>
      <%= f.hidden_field :user_id, :value => @otheruser.id %>
      <%= f.submit "Follow", class: "btn btn-primary" %>
 <% end %>

在控制器创建动作

def create
    user = User.find(params[:user_id])
    current_user.follow(user)
    redirect_to followuser_url
end
user.rb

中的

has_many :followers, class_name: "Relationship" #-> users following you
has_many :following, class_name: "Relationship", foreign_key: :follower_id, foreign_key: :user_id   

  def follow(other_user)
    relationships.create(user_id: other_user.id)
  end
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

现在当我尝试提交跟随按钮时,它显示错误为 “找不到'id'=”的用户,参数是

{"utf8"=>"✓","authenticity_token"=>"681q5ft03+WRdqgHagh/gI1mV3uohwaEj1sF8zdTycUAN5yTiVMT/wGCV4tLPRVRRFRA+6mYSS1bXk2ormA/zw==",
 "user"=>{"user_id"=>"7"},
 "commit"=>"Follow",
 "format"=>"7"}

1 个答案:

答案 0 :(得分:1)

您需要重写控制器0

create action

应该工作!!!