Created
July 26, 2013 10:59
-
-
Save ankit8898/6088035 to your computer and use it in GitHub Desktop.
Strong parameters with array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create | |
p "++++++++#{permitted_params.user.inspect}" | |
p "++++++++#{permitted_params.user['role_ids'].inspect}" | |
@user = User.new(permitted_params.user) | |
if @user.save | |
flash[:notice] = 'User was successfully created.' | |
redirect_to edit_admin_user_url(@user) | |
else | |
render :action => 'new' | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#this is a permitted_params file | |
def user | |
params.require(:user).permit(:first_name, :last_name, :email, :language, | |
:access_level_id, :current_group_id, | |
:first_login, :signed_license, | |
:password, :password_confirmation, | |
:read_stories_at, | |
:currency_id, | |
user_groups_attributes: [:group_id,:id, '_destroy'], | |
user_roles_attributes: [:role_id,:id, '_destroy'], | |
:role_ids => []) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
has_many :user_roles, :dependent => :destroy | |
accepts_nested_attributes_for :user_roles, :allow_destroy => true | |
has_many :roles, :through => :user_roles | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<li> | |
<%= hidden_field_tag "user[role_ids][]", nil %> | |
<%= check_box_tag 'user[role_ids][]', role.id, user.blank? ? nil : user.roles.include?(role) ,id: dom_id(role)%> | |
<%= label_tag dom_id(role), "#{role.group.name}: #{role.name}" -%> | |
</li> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good