2015-02-23 49 views
1

我在向用戶創建的團隊中分配當前用戶角色方面存在挑戰。我想分配創建團隊的用戶以後可以更改的隊長角色。
我目前使用has_one關係附帶的create_asociation方法,因爲這實例化了關聯模型的值,我想用當前用戶對其進行實例化,但得到錯誤Can't mass assign protected attribute: captain。 Captain與用戶是自我加入模式,因爲我希望使用captain.teammatesteam.captain。 下面是涉及的模型。在導軌中設置一個ID作爲默認外鍵

用戶和船長模型

class User < ActiveRecord::Base 
has_one :profile 

has_many :teammates, :class_name => "User", :foreign_key => "captain_id" 
belongs_to :captain, :class_name => "User" 

belongs_to :team 

# before_create :build_profile 
after_create :build_default_profile 

accepts_nested_attributes_for :profile 
attr_accessible :email, :password, :password_confirmation, :profile_attributes, :captain_id 

def build_default_profile 
    Profile.create(user_id: self.id) 
end 

has_secure_password 

before_save { email.downcase! } 
before_save :create_remember_token 

VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, 
       uniqueness: { case_sensitive: false } 
validates :password, presence: true, length: { minimum: 6 } 
validates :password_confirmation, presence: true 

private 

    def create_remember_token 
    self.remember_token = SecureRandom.urlsafe_base64 
    end 
end 

小組模型

class Team < ActiveRecord::Base 
has_many :profiles, through: :users 
has_one :captain, :class_name => "User", foreign_key: :captain_id 
has_one :result, as: :result_table 

attr_accessible :teamname, :color, :result_attributes, :captain_attributes 

after_create :build_result_table 
after_create :build_default_captain 
accepts_nested_attributes_for :profiles 
accepts_nested_attributes_for :captain 
accepts_nested_attributes_for :result 

def build_result_table 
    Result.create(result_table_id: self.id, result_table_type: self.class.name) 
end 

def build_default_captain 
    # Team.captain = User 
    # Captain.create(team_id: self.id, captain_id: user.id) 
end 
end 

用戶控制器

class UsersController < ApplicationController 
before_filter :signed_in_user, only: [:index, :edit, :update, :destroy] 
before_filter :correct_user, only: [:edit, :update] 
before_filter :admin_user,  only: :destroy 

def new 
    @user = User.new 
end 

def create 
    @user = User.new(params[:user]) 
    if @user.save! 
    sign_in @user 
    flash[:success] = "Welcome to the JHDC Mini Olympics Web Application; Thanks for singing Up" 
    redirect_to user_profile_path(@user, @profile) 
    else 
    flash[:error_messages] 
    render 'new' 
    end 
end 

def show 
    @user = User.find(params[:id]) 
end 

def index 
    @users = User.paginate(page: params[:page]) 
end 

def edit 
    @user = User.find(params[:id]) 
end 

def update 
    @user = User.find(params[:id]) 
    if @user.update_attributes(params[:user]) 
    flash[:success] = "Profile Updated" 
    redirect_to user_profile_path(@user, @profile) 
    else 
    render 'edit' 
    end 
end 

def destroy 
    User.find(params[:id]).destroy 
    flash[:success] = "User deleted." 
    redirect_to users_url 
end 

private 

    def signed_in_user 
    unless signed_in? 
    store_location 
    redirect_to signin_url, notice: "Please sign in." 
    end 

    def correct_user 
    @user = User.find(params[:id]) 
    redirect_to(root_path) unless current_user?(@user) 
    end 

    def admin_user 
    redirect_to(root_path) unless current_user.admin? 
    end 

    def user_params 
    params.require(:user).permit(:email, :password, :password_confirmation) 
    end 
end 
end 

隊控制器

class TeamsController < ApplicationController 

def new 
    @team = Team.new 
end 

def create 
    @team = Team.new(params[:team]) 
    @captain = @team.create_captain(captain: current_user) 
    if current_user.admin? 
    if @team.save! 
    flash[:success] = "Team created." 
    redirect_to @team 
    else 
    flash[:error_messages] 
    render 'new' 
    end 
    else 
    flash[:error] = "Sorry, you don't have the authority to create a Team" 
    redirect_to current_user 
    end 
end 

def index 
    @teams = Team.paginate(page: params[:page]) 
end 

def show 
    @team = Team.find(params[:id]) 
end 

def edit 
    if current_user.admin? 
    @team = Team.find(params[:id]) 
    else 
    flash[:error] = "Sorry you dont have the authourity to edit a Team" 
    redirect_to current_user 
    end 
end 

def update 
    @team = Team.find(params[:id]) 
    if @team.update_attributes(params[:team]) 
    flash[:success] = "Team Updated" 
    redirect_to @team 
    else 
    render 'edit' 
    end 
end 

def destroy 
    Team.find(params[:id]).destroy 
    flash[:success] = "Team is deleted." 
    redirect_to teams_url 
end 


private 

    def team_params 
    params.require(:team).permit(:teamname, :color) 
    end 
end 

管理員目前是我用來限制可以創建團隊的用戶的一種方式,但我打算使用像declarative authorization這樣的寶石來創建基於角色的授權。由於

回答

0

你所得到的錯誤是因爲屬性:隊長未聲明爲attr_accessible

要麼設置屬性:隊長在attr_accessible的列表中的用戶模型,或更改代碼的形式

Captain.create(team_id: self.id, captain_id: user.id) 

captain = Captain.new 
captain.team_id = self.id 
captain.captain_id = user.id 
captain.create 
這樣

,屬性不會被大規模分配設置,也不會引發錯誤


編輯

檢查你的代碼兩次後,才意識到你沒有隊長的模式,實際上是:隊長是用戶,並從團隊到用戶的關係的關係。

所以在組隊模式,脫下build_default_captain東西和after_create:build_default_captain,我想說的東西來代替像

after_save :set_default_captain 

def set_default_captain 
    if captain_id_changed? 
    profiles.each do |user| 
     user.captain = captain 
     user.save 
    end 
    end 
end 

所以每次對模型中的captain_id變化,你改變的captain_id它的所有配置文件(用戶)

那麼球隊控制器上,行動上創造的,而不是

@team = Team.new(params[:team]) 
@captain = @team.create_captain(captain: current_user) 

做點什麼LIK, Ë

@team = Team.new(params[:team]) 
@team.captain = current_user 
if current_user.admin? 
    if @team.save! 
    current_user.update_attribute(:team_id, @team.id) 
    flash[:success] = "Team created." 
    redirect_to @team 
    else 
    flash[:error_messages] 
    render 'new' 
    end 
else 
    flash[:error] = "Sorry, you don't have the authority to create a Team" 
    redirect_to current_user 
end 

等代碼的最後一部分,您可以設置球隊以當前用戶的隊長,並設置用戶隊伍,以目前球隊一旦其保存,還可以改善與CURRENT_USER的代碼。 build_team以避免保存current_user。update_attribute

+0

我明白你的邏輯,@rorra,但我現在得到錯誤'未初始化的常量Team :: Captain'。 – 2015-02-23 22:25:02

+0

該線'captain_id_changed? '拋出一個錯誤,它被視爲一種沒有定義的方法。請解釋那部分。 – 2015-02-23 22:56:05

+0

知道了,看到你的評論後添加了新的答案 – rorra 2015-02-23 22:57:17