用户团队Rails应用程序

用户团队Rails应用程序

本文介绍了用户团队Rails应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建用户团队。用户属于​​一个团队(只有一支球队),一支球队拥有很多用户。我无法弄清楚如何让用户能够创建,加入和离开一队。下面是我迄今为止,但我敢肯定,我在做什么可怕的(和纽比错误的)。

I need to create teams of users. A user belongs to a team (only one team) a team has many users. I can't figure out how to get a user to be able to create, join, and leave a team. Below is what I have so far, but I'm sure I'm doing something terribly (and "newby" wrong).

用户模型:

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模型

team_relationship model

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:

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

_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

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

如果你不能告诉我在努力适应一些什么在哈特尔的教程用于这一目的。我究竟做错了什么?

If you can't tell I was trying to adapt some of what's in Hartl's tutorial for this purpose. What am I doing wrong?

我相信我已经得到了模型想通了,但现在我不知道如何让用户创建一个团队,摧毁一个团队,加入一个团队,或留下一队。我有什么做的模型,控制器和视图来做到这一点?

I believe I have gotten the models figured out, but now I'm not sure how to get a user to create a team, destroy a team, join a team, or leave a team. What do I have to do in the models, controllers, and views to make it happen?

推荐答案

我想它了。构建方法是行不通的。团队已经成立,并且用户已经确立。我只需要建立连接。这是我有:

I figured it out. The build method wouldn't work. The team was already established, and the user was already established. I just needed to establish the connection. Here's what I have:

团队控制器:

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

加入按钮操作的部分:

Join Button Partial:

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

这篇关于用户团队Rails应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:12