I need a straightforward, easy to understand answer on this one. I\'m building a user control panel for administrative users to create/edit/delete users of the system throu
Basically everything EmFi suggested, plus a few tweaks:
app/views/users/new.html.haml
- if @roles
%li
- @assigned_roles ||= []
- @roles.each do |role|
= label_tag role
= check_box_tag 'user[assigned_roles][]', role, (@roles & @assigned_roles ).include?(role)
removed:
= hidden_field_tag 'user[assigned_roles][]', @type
(The parameter I wanted to carry over is supplied by:
- form_for @user, :url => {:action => :create, :type => @type) do |f| ...
Since i only used it for presentation, I don't need to store it in the form.)
app/controllers/users_controller.rb:
def new
@user = User.new
@type = params[:type]
@roles = get_all_roles # don't need to reject anything here either, since the :type is no longer part of this array
@cancel = users_path
end
def create
@user = User.new(params[:user])
@assigned_roles = params[:user][:assigned_roles] # already has the roles I need; unchecked checkboxes are not passed into this; only checked ones
if @user.save
update_user_roles(@user,@assigned_roles)
if current_user.is_admin_or_root?
flash[:message] = "User \"#{@user.username}\" created."
redirect_to users_path
else
flash[:message] = "Congrats! You’re now registered, #{@user.username}!"
redirect_to app_path
end
else
@type = params[:type]
@roles = get_all_roles # don't need to reject anything, since the :type is no longer part of this array
@cancel = users_path
render :action => 'new'
end
end
Thanks, EmFi, for setting me straight on this! The @assigned_roles
logic and
- form_for @user, :url => {:action => :create, :type => @type) do |f| ...
were the keys to this puzzle I was searching for!