0

我刚刚完成了 Hartl RoR 教程 http://ruby.railstutorial.org/book并试图添加用户加入组的能力。我的代码基于以下用户的代码。我正在使用属于组和用户的成员资格模型。我能够通过以下方式在 rspec 中创建成员资格:

let(:group) { FactoryGirl.create(:group)}
let(:member) { FactoryGirl.create(:user) }
let(:membership) {group.memberships.build(member_id: member.id, group_id: group.id) }

但是,每当我尝试在实际生产代码中创建成员资格时,group.id 总是最终等于当前用户的用户 ID。例如,如果我尝试以用户 105 的身份加入组 1,我会在运行服务器的终端中获得以下输出。

Started POST "/memberships" for 127.0.0.1 at 2013-11-08 21:07:56 -0600
Processing by MembershipsController#create as JS
  Parameters: {"utf8"=>"✓", "membership"=>{"group_id"=>"1"}, "commit"=>"Join"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."remember_token" = '342a18004399db9cb75e1a7239dc634b2006e7d6' LIMIT 1
{"utf8"=>"✓", "membership"=>{"group_id"=>"1"}, "commit"=>"Join", "action"=>"create", "controller"=>"memberships"}
  Group Load (0.2ms)  SELECT "groups".* FROM "groups" WHERE "groups"."id" = ? LIMIT 1  
[["id", "1"]]
   (0.1ms)  begin transaction
  SQL (0.7ms)  INSERT INTO "memberships" ("created_at", "group_id", "member_id", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", Sat, 09 Nov 2013 03:07:56 UTC +00:00, ["group_id", 105], ["member_id", 105], ["updated_at", Sat, 09 Nov 2013 03:07:56 UTC+00:00]]
   (2.4ms)  commit transaction
  Rendered groups/_join.html.erb (2.9ms)
  Rendered memberships/create.js.erb (10.3ms)
  Completed 200 OK in 32ms (Views: 19.9ms | ActiveRecord: 3.7ms)

用于加入组的按钮的 HTML.erb 部分:

<%= form_for(@group.memberships.build(member_id: current_user.id, group_id: @group.id), remote: true) do |f| %>
  <div><%= f.hidden_field :group_id %></div>
  <%= f.submit "Join", class: "btn btn-large btn-primary" %>
<% end %>

上的页面是组显示路径:

def show
  @group=Group.find(params[:id])  
  @user=current_user
  @members=@group.members.paginate(page: params[:page])
end

成员资格控制器创建操作是:

  def create
    puts params
    @group = Group.find(params[:membership][:group_id])
    #Membership.new(member_id: current_user.id, group_id: @group.id)
    current_user.join!(@group)
  end

会员制模型为:

class Membership < ActiveRecord::Base
  belongs_to :member, class_name: "User"
  belongs_to :group, class_name: "Group"
  validates :member_id, presence: true
  validates :group_id, presence: true
end

用户加入!方法是:

 def join!(group)
   memberships.create!(group_id: group.id, member_id: self.id)
 end

用户模型是:

class User < ActiveRecord::Base
  validates :name, presence: true, length: {maximum: 50}
  VALID_EMAIL_REGEX =  /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
                 uniqueness: {case_sensitive: false }
  before_save {email.downcase!}
  before_create :create_remember_token
  has_secure_password
  validates :password, length: { minimum: 6 }
  has_many :microposts, dependent: :destroy
  has_many :relationships, foreign_key: "follower_id", dependent: :destroy
  has_many :followed_users, through: :relationships, source: :followed
  has_many :reverse_relationships, foreign_key: "followed_id",
                               class_name:  "Relationship",
                               dependent:   :destroy
  has_many :followers, through: :reverse_relationships, source: :follower
  has_many :memberships, foreign_key: "group_id"
  has_many :joined_groups, through: :memberships, source: :group

 def following?(other_user)
   relationships.find_by(followed_id: other_user.id)
 end

 def follow!(other_user)
   relationships.create!(followed_id: other_user.id)
 end

 def unfollow!(other_user)
   relationships.find_by(followed_id: other_user.id).destroy!
 end

 def join!(group)
   memberships.create!(group_id: group.id, member_id: self.id)
 end

 def member?(group)
   memberships.find_by(group_id: group.id)
 end

 def leave_group!(group)
   memberships.find_by(group_id: group.id).destroy
 end

 def User.new_remember_token
   SecureRandom.urlsafe_base64
 end

 def feed
   Micropost.from_users_followed_by(self)
 end

 def User.encrypt(token)
   Digest::SHA1.hexdigest(token.to_s)
 end

 private

   def create_remember_token
     self.remember_token = User.encrypt(User.new_remember_token)
   end
end

最后一点,我以用户 1 的身份加入了组 1,该组现在显示用户 1 的 gravatar,但不是我尝试加入组 1 的其他用户的头像。

4

0 回答 0