Rails 3 app中的团队

时间:2012-09-05 06:03:00

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

我在这另外1次发布了,但它仍然无法正常工作,尽管尝试了3个小时,但我无法弄清楚原因。我对这一切都很陌生,这对我来说非常复杂。我非常感谢你的一些帮助。谢谢!

我需要创建一个用户团队。用户belongs_to一个团队,一个团队has_many用户。

用户应该能够创建一个团队,加入已有的团队,加入他们当前的团队(如果您可以告诉我如何让创建团队的人员成为团队领导者,那也很棒! )

我的数据库表设置如下:

  • 用户表:id, name, email, timestamps, team_id
  • 团队表:id, team_name, timestamps, user_id

我得到的错误:

undefined method `team_build' for #<User:0x3a14a80>

1: <%= button_to "Join", join_teams_path(current_user.team.build(team_id: @team_id)),    class: "btn btn-large btn-primary" %>

用户模型:

class User < ActiveRecord::Base
attr_accessible :company, :name, :email, :password, :password_confirmation, :image
    belongs_to :team, dependent: :destroy

    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!(team)
    return if team_member?
    team.destroy
   end

团队模型:

class Team < ActiveRecord::Base
has_many :users

attr_accessible :team_name

before_save { |team| team.team_name = team_name.downcase }

validates :team_name, 
        presence: true, 
        length: { maximum: 140 }, 
        uniqueness: { case_sensitive: false }

default_scope order: 'teams.created_at DESC'

end

团队控制器:

class TeamsController < ApplicationController
 before_filter :signed_in_user

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 show
 @team =  Team.find(params[:id])
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

def new
  @team = Team.new
end

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

 def teams
  @title = "Teams"
  @team = Teams.find(params[:id])
  render 'show_teams'
 end

 def index
Team.all
 end

加入按钮:

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

取消加入按钮:

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

路线:

resources :teams

match '/teams',          to: 'teams#index'
match '/new_team',       to: 'teams#new'

3 个答案:

答案 0 :(得分:2)

current_user.teams.build(whatever)
如果你有一个has_many关系,

就是你这样做的方式。但是如果你有belongs_to关联,你需要:

current_user.build_team(whatever)

有关详情,请参阅belongs_to here的文档。

答案 1 :(得分:1)

“(如果你能告诉我如何让创建团队的人成为团队领导者,那也很棒!)”

对于团队领导,您可以通过几种方式实现。这是一个简单的方法。

$ rails generate migration AddTeamLeaderUserIdToTeams team_leader_user_id:integer
$ rake db:migrate

这样做是为您的团队表添加一个新列,以便每个团队可以存储其团队负责人的用户ID(注意:这意味着每个团队只能有一个团队负责人。我可以解释您是否想要如何拥有多个团队领导者。)

从那里,当你创建一个新团队时,你可以这样做:

team = Team.create({:team_name => "Stanford Cardinal", :team_leader_user_id => current_user.id}). 

然后将来你可以得到团队领导:

team_leader = User.find_by_id(Team.find_by_team_name("Stanford Cardinal").team_leader_user_id))

希望这有点帮助。如果不清楚,请随意提出更多问题。

答案 2 :(得分: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 %>