2014-10-20 64 views
0

您好,我需要幫助用戶在註冊帳戶時推送用戶。經過漫長的艱苦的一天,我成功地將組的嵌套屬性添加到用戶。但現在當我提交表單我得到這個錯誤...如何讓用戶在註冊帳戶時成爲羣組

ActiveRecord::AssociationTypeMismatch at /users Group(#70300311693200) expected, got ActionController::Parameters(#70300262460820) 

這裏是我的模型項目

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable 
    belongs_to :group 
    end 

    class Group < ActiveRecord::Base 
    validates :name, presence: true 
    has_many :users 
    default_scope lambda { order('groups.name') } 
    accepts_nested_attributes_for :users 
    end 

這裏是我的嵌套屬性視圖

 <div class="form-group"> 
     <%= f.fields_for :group do |i| %> 
     <%= i.label :name %><br /> 
     <%= i.select :name , Group.all.map { |c| [c.name, c.id] }%></p> 
     <% end %> 
    </div> 

這裏是我的應用控制器

class ApplicationController < ActionController::Base 
    # Prevent CSRF attacks by raising an exception. 
    # For APIs, you may want to use :null_session instead. 
    protect_from_forgery with: :exception 
    before_filter :configure_permitted_parameters, if: :devise_controller? 

    protected 

    def configure_permitted_parameters 
     devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :first, :last, group: [:name, :id]) } 
     devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :last, :first, :current_password, :password, group: [:name, :id]) } 
     end 
    end 

這是我的遷移文件

class CreateJoinTableUserGroup < ActiveRecord::Migration 
     def change 
     create_join_table :users, :groups do |t| 
     t.index [:user_id, :group_id] 
     t.index [:group_id, :user_id] 
     end 
    end 
    end 

更新

class AddGroupIdToUser < ActiveRecord::Migration 
    def change 
     add_column :users, :group_id, :integer 
    end 
    end 

所有我需要的是能夠檢查我的rails控制檯,可以看到如果用戶與組關聯。如果有人能告訴我一個快速的方法來做到這一點,我將不勝感激。我正在使用rails 4.1.6和設計。

回答

0

提供您對用戶一個GROUP_ID外鍵其值等於給集團主ID的一個,你應該能夠通過輸入

Group.first.users 
     OR 
Group.find(ID).users 
測試軌控制檯上這種關係

參考http://guides.rubyonrails.org/association_basics.html#the-has-many-association欲瞭解更多關於has_many協會的信息

+0

@uriu我已經編輯過這個問題,現在它包含遷移文件。也說哇這是一個很好的方法。現在我知道我的遷移文件是錯誤的,或者至少需要更改。當運行這個方法時,我收到了errorSQLite3 :: SQLException:no such column:users.group_id:SELECT「users」。* FROM「users」WHERE「users」。「group_id」=? ActiveRecord :: StatementInvalid:SQLite3 :: SQLException:no such column:users.group_id:SELECT「users」。* FROM「users」WHERE「users」。「group_id」=?。也只是閱讀指南,並將實施它們。您認爲遷移文件有什麼錯誤? – 2014-10-22 18:40:33

+0

@denzy,您的遷移代碼用於創建一個連接表,如果您有HABTM關係,則使用該連接表。請參閱上面的更新遷移以在用戶表上創建group_id列。 – alotofnoodles 2014-10-23 12:44:14

1

你應該有這種概念的連接表。

由於您正在使用設計進行註冊和其他目的。您需要覆蓋設計的視圖和控制器以進行所需的更改。因爲我認爲你應該在你的sign_up表格上添加feilds_for組來添加組中的用戶。

+0

起初我不想住在設計控制器,但我意識到我可能不得不。你有設計控制器的經驗嗎?我仍然猶豫要不要這樣做,而且真的沒有必要。不過,我知道可以嘗試一下。我會嘗試編輯控制器,並讓你知道它是否適合我。非常感謝你的回答。 – 2014-10-20 16:41:23

+0

@ user3952353好消息是我想出瞭如何在設計視圖中包含嵌套屬性,但是現在我現在正在接收此錯誤。 ActiveRecord :: AssociationTypeMismatch在/用戶組(#70300311693200)預計,得到ActionController :: Parameters(#70300262460820)。這裏是我的新代碼在github https://github.com/DenzzyDrake/Trouper。 – 2014-10-21 04:22:59

+0

@ user3952353我現在編輯了這個問題,以便更好地理解我正在尋找的內容以及我編輯的代碼 – 2014-10-21 04:25:45