Topic: Some reasons flash[:error] may not get reset???

I was doing well with Rails for a few days here and now I've hit a massive brick wall.

I'd post the code for this but I'm tweaking the Login_Sugar and trying to make that damn thing work the way you want it a bear.  All the cryptic helpers make it really hard to provide example code.

Basically, I have an 'edit' user action and a 'change password' action.
I've split them out into two pages, but whatever I do, if I "submit" an empty change_password
form, my password validation errors carry over in BOTH forms until I log out.  Any time I click on a link or go directly to the user/edit or user/change_password pages I get the two validation errors: Password too short & Password can't be blank.

I thought this was the point of "flash" was to last just for the next page.

I'm looking for some general reasons why this might be happening so I know where to start in debugging this sucker.  Any info at all would be helpful.

Thank you

Re: Some reasons flash[:error] may not get reset???

Here are the resulting forms on the two pages and some of the controller code:

edit form:

<form action="/user/edit" method="post">

<input id="user_form" name="user[form]" type="hidden" value="edit" />

<input id="user_first_name" name="user[first_name]" size="30" type="text" value="James" />

<input id="user_last_name" name="user[last_name]" size="30" type="text" value="Brown" />

<input id="user_postal_code" name="user[postal_code]" size="30" type="text" value="90210" />

<input name="submit" value="Change settings" type="image" src="/images/submit.png" alt="Submit" class="submit" />

</form>


change_password form:

<form action="/user/change_password" method="post">

<input id="user_form" name="user[form]" type="hidden" value="change_password" />

<input id="user_password" name="user[password]" size="30" type="password" value="" />

           
<input id="user_password_confirmation" name="user[password_confirmation]" size="30" type="password" value="" />
       

<input name="submit" value="Change password" type="image" src="/images/submit.png" alt="Submit" class="submit" />

</form>


And the user_controller:
  def edit
    return if generate_filled_in
    if params['user']['form']
      @user.attributes = params[:user].delete_if { |k,v| not UserSystem::CONFIG[:changeable_fields].include?(k) }
      @user.save
    end
  end

Re: Some reasons flash[:error] may not get reset???

Sorry premature post...

Here's the rest of user_controller

  def generate_blank
    case @request.method
    when :get
      @user = User.new
      render
      return true
    end
    return false
  end

Re: Some reasons flash[:error] may not get reset???

I think you need to assign to flash.now. I've run into similar problems and sometimes I need to assign to flash.now, sometimes just flash. According to Agile Development With Rails 2nd Ed, flash.now only lasts for the duration of the current request.

Re: Some reasons flash[:error] may not get reset???

Where are you setting flash[:error]? I don't see it in the code provided.

Railscasts - Free Ruby on Rails Screencasts

Re: Some reasons flash[:error] may not get reset???

It's being set by the following validation methods:

user.rb

  validates_presence_of :login, :on => :create
  validates_length_of :login, :within => 3..40, :on => :create
  validates_uniqueness_of :login, :on => :create
  validates_uniqueness_of :email, :on => :create

  validates_presence_of :password, :if => :validate_password?
  validates_confirmation_of :password, :if => :validate_password?
  validates_length_of :password, { :minimum => 5, :if => :validate_password? }
  validates_length_of :password, { :maximum => 40, :if => :validate_password? }

  validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i,
      :message => 'must be valid'
  validates_presence_of :first_name, :last_name
  validates_length_of :postal_code, :within => 5..10

Re: Some reasons flash[:error] may not get reset???

I think it has something to do with:

  def validate_password?
    @new_password
  end

@new_password gets set a few places:

* initialize sets it to false
* change_password sets it to true
* crypt_password looks like this

  after_save '@new_password = false'
  after_validation :crypt_password
  def crypt_password
    if @new_password
      write_attribute("salt", self.class.hashed("salt-#{Clock.now}"))
      write_attribute("salted_password", self.class.salted_password(salt, self.class.hashed(@password)))
    end
  end

Re: Some reasons flash[:error] may not get reset???

The validations add the error messages to the model itself, not flash[:error]. How are you displaying the error messages in the view?

Railscasts - Free Ruby on Rails Screencasts

Re: Some reasons flash[:error] may not get reset???

I've used either

<%= form_errors (@user, 'profile information') %>

  def form_errors (form_object, title = '')
    title = 'Please verify the ' + title + ' information you provided' if !title.include? ' '
    render :partial => 'shared/error_message', :locals => {
           :title => title, :errors => form_object.errors } if form_object.errors
  end


or

<%= error_messages_for 'user' %>

they both yield the same results

Re: Some reasons flash[:error] may not get reset???

The validation error messages shouldn't be persistent across requests (they don't act like flash[]). So either the validations are being triggered where they aren't supposed to be or the object is being stored in a session or something after it is validated. How is the @user instance variable set in the edit action?

Railscasts - Free Ruby on Rails Screencasts

Re: Some reasons flash[:error] may not get reset???

  def edit
    return if generate_filled_in
    if params['user']['form']
          @user.attributes = params[:user].delete_if { |k,v| not UserSystem::CONFIG[:changeable_fields].include?(k) }
          @user.save
    end
  end

  def generate_filled_in
    @user = session['user']
    case @request.method
    when :get
      render
      return true
    end
    return false
  end
end

Re: Some reasons flash[:error] may not get reset???

Well anyway, I'm going to give up on this for a while and wait to use the "LoginEngine", which is apparently fixing a lot of the same bugs I've been trying to fix in Login_Sugar/Salted_Login_Generator...