Twitterのようなフォローモデルを設定しました。ユーザーはすべて相互にサブスクライブできます。リレーションシップを作成しようとすると、ユーザー コントローラーでエラーが発生します。
ユーザー.rb:
has_many :subscriptions
has_many :providers, :through => :subscriptions
has_many :followings, :class_name => "Subscription"
has_many :followers, :through => :followings
サブスクリプション.rb
belongs_to :provider, :class_name => 'User', :foreign_key => "provider_id"
belongs_to :follower, :class_name => 'User', :foreign_key => "follower_id"
users_controller.rb
69 def follow
70 logger.debug params.to_yaml
71 @user = User.find(params["user_id"])
72 logger.debug @user.to_yaml
73 if current_user.providers << @user
74 flash[:notice] = "Subscribed"
75 else
76 flash[:error] = "Unable to subscribe."
77 end
78 end
これは、フォローを呼び出すときのエラーです。
ActiveRecord::UnknownAttributeError (unknown attribute: user_id):
app/controllers/users_controller.rb:73:in `follow'
rake db:migrate を実行したことを確認しました - サブスクリプション テーブルには provider_id と follower_id の 2 つのフィールドがあります。誰でもエラーを手伝って、「user_id」属性を探している理由を説明できますか?
アップデート:
show.html.erb:
<%= button_to "Subscribe", user_follow_path(@user), :remote => true %>
ルート.rb:
resources :users do
resources :locations
resources :saved_events
resources :saved_locations
post "follow"
end
レーキルート | grep フォロー:
user_follow POST /users/:user_id/follow(.:format) {:action=>"follow", :controller=>"users"}