Sandeep Rao
Sandeep Rao

Reputation: 1757

Accessing a user in forgot password method in rails

I'm having a problem in accessing the email of the user in forgotpassword method. How can I access the email without creating a new user in this method. I have send_new_password method in users.rb model.

def forgot_password
    if request.post?
      user = User.find_by_email(params[:user][:email])
      puts user
      if user and user.send_new_password
        flash[:message]  = "A new password has been sent by email."
       # redirect_to root_path
      else
        flash[:warning]  = "Couldn't send password"
      end
    end
  end

Here is my forgot password page

<h1>Forgot Password</h1>
<%= form_for(@user) do |f| %>
  <%= render 'shared/error_messages', :object => f.object %>
    <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
<div class="actions">
    <%= f.submit "Submit" %>
  </div>
<% end %>

Upvotes: 0

Views: 48

Answers (1)

Kelvin
Kelvin

Reputation: 20867

You probably want to be doing

form_for(User.new)

since you won't have any object to put into @user

Upvotes: 1

Related Questions