【问题标题】:Teams of users rails app用户团队 rails app
【发布时间】:2012-08-26 21:44:26
【问题描述】:

我需要创建用户团队。一个用户属于一个团队(只有一个团队)一个团队有很多用户。我不知道如何让用户能够创建、加入和离开团队。以下是我到目前为止所拥有的,但我确信我做的很糟糕(而且“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

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

_unjoin_team.html.erb

<%= form_for(current_user.team_relationships.find_by_team_id(@team_id),
         html: { method: :delete }) do |f| %>
  <%= f.submit "Leave Team", class: "btn btn-large" %>
<% end %>

如果你不知道我是为了这个目的而尝试改编 Hartl 教程中的一些内容。我做错了什么?

我相信我已经弄清楚了模型,但现在我不确定如何让用户创建团队、摧毁团队、加入团队或离开团队。我必须在模型、控制器和视图中做什么才能实现它?

【问题讨论】:

  • 你能贴出 User 模型的代码吗?
  • 它在顶部只是没有正确缩进;)
  • @SteaveQ 是 team_relationships 吗?中间关联模型还是您想将其引用给团队?
  • 我完全不明白你为什么需要一个 team_relationship 模型。简单的belongs_to 和has_many 关系不能解决您的问题?
  • @mdepolli 是的,你是对的..!!

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


【解决方案1】:

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

团队控制器:

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 %>

【讨论】:

    【解决方案2】:

    不确定这是否是您的问题,但

    belongs_to :teams
    

    应该是

    belongs_to :team
    

    Belongs to 总是单数。

    edit:看起来你要建立多对多的关系。试试这个:

    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
    

    我认为应该这样做。

    【讨论】:

    • 我收到“#<0x31a6c88>
    【解决方案3】:

    当你只有一个关系时你不需要has_many, through 作为用户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'
    

    团队关系模型#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

    【讨论】:

    • 我就是这么想的,除非我们缺少一些隐藏的要求,否则这不是多对多关联。
    • 好的,我对模型进行了更改,我认为现在一切都应该很好。我现在在控制器/视图中放置什么以允许用户创建、加入和离开团队?我在那里的东西不起作用(令人震惊,我知道!)感谢所有帮助!
    • @SteveQ,检查更新的答案,根据您的要求进行修改!
    • 感谢大家的帮助!
    • 抱歉回复缓慢。当我可以的时候,我肯定会在晚上做这个。不幸的是,它没有用,我收到以下错误:#<0x3d36218>
    猜你喜欢
    • 2020-07-21
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多