Conditional value for ActiveRecord create method only
- by Steve Wright
I have a form where I have an administrator creating new users. The form uses the User model I created (login, password, first_name, etc...). For the last field on the form, I want to have a checkbox that doesn't need to be stored as part of the User record, but it is needed for the controller. This will control whether or not the newly created user will receive a welcome email or not.
def create
@user = User.new(params[:user])
if @user.save
if @user.send_welcome_email
UserMailer.welcome_email(@user).deliver
end
redirect_to(admin_users_url, :notice => "User #{@user.name} was successfully created.")
else
render :action => "new"
end
end
In my view (haml) I am trying to access it like this:
%p
Send Welcome Email?
= f.check_box :send_welcome_email
I tried to make this an attr_accessible: :send_welcome_email but the controller does not recognize it. I get an
undefined method 'send_welcome_email' for #<User:0x00000100d080a8>
I would like it to look like this:
What is the best way to get this working: