用户团队应用程序

我需要创建用户团队。 用户属于团队(仅一个团队),团队拥有许多用户。 我无法弄清楚如何让用户能够创建,加入和离开团队。 以下是我到目前为止所做的事情,但我确信我做得非常糟糕(并且“newby”错误)。

用户模型:

belongs_to :teams, dependent: :destroy def team_member?(team) team_relationships.find_by_team_id(team.id) end def join!(team) team_relationships.create!(team_id: team.id) end def unjoin!(team) team_relationships.find_by_team_id(team.id).destroy end 

团队模型

 has_many :users, through: :team_relationships, dependent: :destroy attr_accessible :team_name, :team_id validates :user_id, presence: true validates :team_name, presence: true, length: { maximum: 140 } default_scope order: 'teams.created_at DESC' 

team_relationship模型

 attr_accessible :team_id belongs_to :team belongs_to :user validates :team_id, presence: true 

路线:

  resources :teams do member do get 'join' get 'leave' end end 

teams_controller:

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

_join_team.html.erb

  

_unjoin_team.html.erb

    

如果你不能说我试图调整Hartl教程中的一些东西来达到这个目的。 我究竟做错了什么?

我相信我已经找到了模型,但现在我不确定如何让用户创建团队,摧毁团队,加入团队或离开团队。 我需要在模型,控制器和视图中做些什么来实现它?

我想到了。 构建方法不起作用。 该团队已经建立,用户已经建立。 我只需要建立连接。 这就是我所拥有的:

团队控制器:

 def show @team = Team.find(params[:id]) @team_members = @team.users @user = User.find(params[:id]) if signed_in? end 

加入按钮部分:

 <%= form_for(@user) do |f| %> <%= f.hidden_field :team_id, :value => @team.id %> <%= f.submit "Join Team", class: "btn btn-large btn-primary" %> <% end %> 

不确定这是不是你的问题但是

 belongs_to :teams 

应该

 belongs_to :team 

属于永远是单数。

编辑:看起来你想要多对多的关系。 试试这个:

 class User has_many :team_relationships, dependent: :destroy has_many :teams, through: :team_relationships end class Team has_many :team_relationships, dependent: :destroy has_many :users, through: :team_relationships end class TeamRelationship belongs_to :user belongs_to :team end 

我认为应该这样做。

如果你只有一个关系,你不需要has_manythrough用户belongs_to只有one团队和团队有many用户它有一个has_many – > belongs_to关系

用户模型:

 belongs_to :team validates :user_id, presence: true def team_member? team.present? end def join!(team) return false if team_member? team.create!(team_id: team.id) end def unjoin! return if team_member? 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' 

TeamRelationship模型#NOT NEEDED

_join_team.html.erb

 <%= button_to "Join", join_teams_path(current_user.team_build(team_id: @team_id), class: "btn btn-large btn-primary", remote: true %> 

_unjoin_team.html.erb

 <%= button_to "Leave Team", leave_teams_path(current_user.team) , class: "btn btn-large btn-primary" , remote: true %> 

team_controllor

  def join @team = Team.find params[:id] if current_user.join!(@team.id) redirect_to @user #NOTE dont use redirect when you perform actions with ajax. else #render some error end end def leave if current_user.unjoin! redirect_to @user #NOTE dont use redirect when you perform actions with ajax. else #render some error end end 

我建议你通过rails定义一些文档

http://guides.rubyonrails.org/ajax_on_rails.html

http://railscasts.com/episodes/205-unobtrusive-javascript

http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html

http://guides.rubyonrails.org/association_basics.html