2012-08-26 81 views
1

我需要創建用戶團隊。用戶屬於一個團隊(只有一個團隊),一個團隊有許多用戶。我無法弄清楚如何讓用戶能夠創建,加入和離開團隊。下面是我到目前爲止,但我敢肯定,我正在做一些可怕的事情(和「新的」錯誤)。用戶團隊Rails應用程序

用戶模型:

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的教程爲此目的。我究竟做錯了什麼?

我相信我已經弄清了模型,但現在我不確定如何讓用戶創建團隊,摧毀團隊,加入團隊或離開團隊。我需要在模型,控制器和視圖中做什麼才能實現?

+0

您可以發佈爲用戶模型的代碼? – depa

+0

它在頂部只是沒有正確縮進;) –

+0

@SteaveQ是team_relationships?一箇中間誹謗模式,或者你想參考團隊? – PriteshJ

回答

2

我想通了。構建方法不起作用。該團隊已經建立,用戶已經建立。我只需要建立連接。下面是我有:

隊控制器:

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

不知道這是你的問題,但

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 

我認爲應該這樣做。

+0

我得到了「未定義的方法'team_relationships'爲#<用戶:0x31a6c88>」我不知道爲什麼會發生這種情況?我將其改爲團隊,但我無法檢查是否有效。 –

+0

哦,我看,它應該是一個has_many:通過,讓我更新我的答案.. –

0

當你只有一個關係的船,你不需要has_manythrough 爲用戶belongs_toone隊ANS團隊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 model#不需要

_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 

我建議你去通過一些文檔從軌道他們是有嚴格規定

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

+0

@SteveQ,我已經更新了答案檢查出 – PriteshJ

+0

這就是我認爲,除非我們錯過了一些隱藏的要求,這不是一個多對多的關聯。 – depa

+0

好的我對模型做了修改,現在我覺得所有的東西都應該很好。現在我在控制器/視圖中放置什麼來允許用戶創建,加入和離開團隊?我在那裏有沒有工作(令人震驚,我知道!)感謝所有的幫助! –