组创建时,User_id始终为零

时间:2017-05-30 23:10:12

标签: ruby-on-rails

我的群组控制器出现问题。我可以创建新组,但user_id始终为零。我认为这与我设置关联的方式有关,请参阅此处我如何拥有:创建者部分

Group.rb

  belongs_to :creator, class_name: "User"
  has_many :memberships
  has_many :users, through: :memberships

User.rb

  has_many :created_groups, class_name: "Group"
  has_many :memberships
  has_many :groups, through: :memberships

groups_controller.rb

  def new
    @group = current_user.groups.build
  end

  def create
    @group = current_user.groups.build(group_params)

    respond_to do |format|
      if @group.save
        format.html { redirect_to @group, notice: 'You have a new Group!' }
      else
        format.html { render :new }
      end
    end
  end

如何设置此选项以便使用当前设置传递当前用户ID?

1 个答案:

答案 0 :(得分:1)

应该是

@group = current_user.created_groups.build(group_params)