简体   繁体   中英

How to let Users Be Apart Of A Group When They Sign Up for an Account

Hello I need help pushing users into groups when they sign up for an account. After a long hard day I have succeeded in adding the nested attribute of groups to users. But now when I submit my form I get this error...

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

Here are my models for the project

  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

Here is my nested attribute view

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

And here is my application controller

 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

And Here is My Migration File

    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

Update

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

All I need is to be able to check my rails console and be able to see if Users are Associated with a Group. If any one can tell me a quick method to do so I would greatly appreciate it. I am using rails 4.1.6 and devise.

You should have join table for this kind of concept.

As you are using devise for registration and other purpose. You need to override views and controller of devise to make desired changes. Because I think you should add feilds_for group on your sign_up form to add user in a group.

Providing you have a group_id foreign key on User which its value equal to to one of the Group's primary id, you should be able to test this relationship on rails console by typing

Group.first.users 
       OR
Group.find(ID).users  

Refer to http://guides.rubyonrails.org/association_basics.html#the-has-many-association for more info about has_many association

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM