创建一个团队rails 3

时间:2012-08-27 04:25:41

标签: ruby-on-rails ruby-on-rails-3

用户属于团队,团队拥有多个用户。用户一次只能属于一个团队。我希望用户能够创建团队,加入现有团队或离开现有团队。以下是我现在在模型,控制器和视图中的内容,但它不起作用。

另外,我希望创建团队的人成为团队负责人(管理员)。有人可以告诉我怎么做吗?

用户模型:

belongs_to :team, dependent: :destroy


def team_member?
 team.present?
end

def join!(team)
 team.create!
end  

def unjoin!(team)
 team.destroy
end

团队模型:

 has_many :users

 attr_accessible :team_name, :team_id

 validates :team_name, presence: true, length: { maximum: 140 }

 default_scope order: 'teams.created_at DESC'

团队控制员:

before_filter :signed_in_user

def join
  @team = Team.find params[:id]
  current_user.update_attribute(:team_id, @team.id)
  redirect_to @team
end

def leave
  @team = Team.find params[:id]
  current_user.update_attribute(:team_id, nil)
  redirect_to @team
end

  def create
    @team = Team.new(params[:team])
    if @team.save
      flash[:success] = "Team Created!"  
      redirect_to @team
    else
     render 'new'
    end
  end

_join_team_.html.erb

<%= form_for(current_user.team.join) do |f| %>
  <div><%= f.hidden_field :team_name %></div>
  <%= f.submit "Join", class: "btn btn-large btn-primary" %>
<% end %>

1 个答案:

答案 0 :(得分:0)

协会似乎很好。处理代码。要添加“teamlead”,您可以将has_one关联添加到团队模型:

has_one :teamlead, :class => "User", :dependent => :nullify