我已经建立了一个类似推特的追随者模型。用户都可以互相订阅。尝试创建关系时,我的用户控制器中出现错误。
用户.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。谁能帮我解决这个错误并解释它为什么要寻找“user_id”属性?
更新:
显示.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"}