Skip to content

Instantly share code, notes, and snippets.

@ankit8898
Created July 26, 2013 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ankit8898/6088035 to your computer and use it in GitHub Desktop.
Save ankit8898/6088035 to your computer and use it in GitHub Desktop.
Strong parameters with array
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 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 => [])
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
<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>
@zhuangbifan
Copy link

good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment