Topic: Um, parse DOM from rails?

Hi. Actually I'm not sure I really need this smile

I have a login screen with autocomplete for user names like here. When user selects the name, his email goes into a hidden field. I.e., "user name" text box has the name but users get authorized by their emails.

Then, I want to implement a "forgot password" feature.
And wanna have it as a link on the login screen. So the user wouldn't need to go to another page and submit an email to retrieve a regenerated password. I'd like them just to click on the "regenerate password" link and have an updated password emailed.

So here goes the code

View:[code="ruby"]
<% form_tag({:action => 'login'}) do %>
Please login
<%= hidden_field 'user', 'email' %>
User: <%= text_field_with_auto_complete :user, :name, {}, :skip_style => true, :after_update_element => 'applyEmail' %><br/>
Password: <%= password_field('user', 'password', {'size' => 30, 'class' => 'input'}) %><br/>           
<input type='submit' class='submit' value='Login'/>
<br/>
<br/>
<%= link_to 'Reset my password', {:action => 'resetpassword', :params => { :email => 'what-to-put-here??' }}, :method => :post %>
<% end %>
[/code]
So, I don't know how to get the email from html that is updated by javascript on the fly.
applyEmail function looks like
[code="javascript"]
function applyEmail(element, value) {
var text = value.childNodes.item(1).innerHTML
text = text.substring(text.indexOf('>') + 1, text.length)
var email = text.substring(0, text.indexOf('<'))
var emailField = document.getElementById('user_email');
if (emailField)
  emailField.value = email;
}
[/code]

and controller will be like [code="ruby"]
def resetpassword
   if request.post?
      user = User.find_by_email(params[:email])
      if user and user.send_new_password
    flash['message']  = 'A new password has been sent by email'
      else
    flash['warning']  = 'Incorrect user name or mail could not be delivered'
      end
   end
   redirect_to :action => 'login'
end
[/code]

Is there any nice way to do what I want?

Last edited by andreister (2007-08-10 13:21:56)

Re: Um, parse DOM from rails?

So, no way to do that?

Re: Um, parse DOM from rails?

Ok, I found the way.
Instead of link_to 'Reset my password' (line #9 in the code above) I use [code="ruby"]
<%= link_to_remote 'Reset my password',
    :url => {:action => 'resetpassword'},
    :submit => 'user_email_div',
    :before=>'showMessage("A new password has been sent to your email");', 
    :failure=>'showMessage(request.responseText);' %>[/code]while having wrapped the hidden email field with the "user_email_div" div so that the only this div to be sent asynchronously to the action in the controller.

Last edited by andreister (2007-08-22 01:24:04)