2014-10-20 88 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