2 種類のユーザーがいます。1 つは list_owner と呼ばれ、もう 1 つは Subscriber と呼ばれます。これらは両方ともdeviseで作成されたユーザーです。サブスクライバーは、list_owner をフォローできる必要があります。サブスクライバーが誰をフォローしているかを追跡するために、Relationship という別のモデルがあります。
主に例としてhttp://ruby.railstutorial.org/を使用しました。
これらは関係です
class Relationship < ActiveRecord::Base
attr_accessible :list_owner_id, :subscriber_id
belongs_to :list_owner
belongs_to :subscriber
end
class ListOwner < ActiveRecord::Base
has_many :messages
has_many :relationships, :dependent => :destroy, :foreign_key => "list_owner_id"
end
class Subscriber < ActiveRecord::Base
has_many :relationships, :foreign_key => "subscriber_id",
:dependent => :destroy
def follow!(list_owner)
puts "-----------------------"
relationships.create!(:subscriber_id => subscriber.id, :list_owner_id => list_owner.id)
end
end
購読者が購読できるボタンを備えたリスト所有者のリストを作成しました。
<%= form_for current_subscriber.relationships.build(:list_owner_id => l.id, :subscriber_id => @subscriber.id),
:remote => true do |f| %>
<%= f.hidden_field :list_owner_id %>
<%= f.hidden_field :subscriber_id %>
<%= f.submit "Subscribe", :class => "button btn" %>
<% end %>
class RelationshipsController < ApplicationController
def create
puts "---------------------relationships CREATE---------------------"
@list_owner = ListOwner.find(params[:relationship][:list_owner_id])
current_subscriber.follow!(@list_owner)
redirect_to root_path
end
end
これは私が得るエラーです。私は何を間違っていますか?
Started POST "/relationships" for 127.0.0.1 at Wed Sep 07 15:37:17 +0200 2011
Processing by RelationshipsController#create as JS
Parameters: {"commit"=>"Subscribe", "relationship"=>{"list_owner_id"=>"2", "subscriber_id"=>"1"}, "authenticity_token"=>"m5plsJLdzuwNt1yKfDJFKD28GcR138V+pezbfbECCPk=", "utf8"=>"✓", "_"=>""}
ListOwner Load (0.2ms) SELECT "list_owners".* FROM "list_owners" WHERE "list_owners"."id" = 2 LIMIT 1
Subscriber Load (0.2ms) SELECT "subscribers".* FROM "subscribers" WHERE "subscribers"."id" = 1 LIMIT 1
Completed 500 Internal Server Error in 34ms
NameError (undefined local variable or method `subscriber' for #<Subscriber:0x102f140c0>):
app/models/subscriber.rb:21:in `follow!'
app/controllers/relationships_controller.rb:8:in `create'