Problem with submit form

I have the general submit form with info like address, email, name,
etc.

However I seem to stumbled into a problem, everytime I access that
page, rails validates if the fields are empty or not. I have this
code ....

def new_user
    # if pressed submit ....
    @user = User.new(params[:user])
    @user.some_bool_option = true
    if @user.save
      flash[:notice] = 'User was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new_user'
    end
end

What I would like to see if it's possible to create the user instance
ONLY if the user pressed the submit tag. Is this even possible?

The usual idiom would be:

  def new_user
    @user = User.new
    if request.post?
      @user.attributes = params[:user]
      if @user.save
        flash[:notice] = 'User was successfully created.'
        redirect_to :action => 'list'
        return
      end
    end
  end

This makes a GET render with a blank form, while a POST will redirect
if successful, or render with the user's values carrying forward (so
errors can be corrected). You can use error_messages_for(:user) in the
view to show the errors.

Thanks for the tip, but it does not work. After I input the data in the field and finally press the submit button, it clears ALL data and gives me the validate_presence_off errors.

Is it actually submitting the data the way you think it is (ie what is in your form ?)

Fred

Create Admin Account

<%= error_messages_for 'user' %>

<%= start_form_tag :action => ‘new_admin’ %>

Username

<%= text_field ‘user’, ‘username’ %>

Password
<%= password_field 'user', 'password' %>

<%= submit_tag “Create Account” %>

<%= end_form_tag %>

This is in my form. It still does the same thing, once I press submit, it clears all fields and reports the verify_presence_of errors.

I'd check that the data being submitted is what you think it is.

Fred

The data is what I think it is …

Are you sure this is the right form ? you're working on your new_user action, but this is a form for new_admin

Fred

Yes, im sure it’s the right form, I copied and pasted from an older source. But tis the right form.

Found the problem. I forgot to send the params :stuck_out_tongue: My mistake :slight_smile: