Rodolphe Geant
Rodolphe Geant

Reputation: 89

Unable to redirect to password reset form - The Rails Tutorial by Michael Hartle

I'm posting this message after searching and searching for an already posted similar problem, but after several days of frustration, I have to come for help! I'm a newbie developer and I, frankly, don't understand what I'm doing most of the times. HOWEVER I am stubborn, and I want to become a good ruby developer. So here is my question:

I keep getting the following error message in production:

2016-12-12T06:40:48.173693+00:00 app[web.1]: F, [2016-12-12T06:40:48.173631 #4] FATAL -- : [f6758f60-0e48-4a7f-8d4d-cafca412aa1c] ActionView::Template::Error (undefined local variable or method `object' for #<#<Class:0x007f57040214f0>:0x007f570e8b9fb8>
2016-12-12T06:40:48.173694+00:00 app[web.1]: Did you mean?  object_id):
2016-12-12T06:40:48.173841+00:00 app[web.1]: F, [2016-12-12T06:40:48.173794 #4] FATAL -- : [f6758f60-0e48-4a7f-8d4d-cafca412aa1c]     1: 
2016-12-12T06:40:48.173865+00:00 app[web.1]: F, [2016-12-12T06:40:48.173832 #4] FATAL -- : [f6758f60-0e48-4a7f-8d4d-cafca412aa1c]     2:   <% if object.errors.any? %>
2016-12-12T06:40:48.173922+00:00 app[web.1]: F, [2016-12-12T06:40:48.173865 #4] FATAL -- : [f6758f60-0e48-4a7f-8d4d-cafca412aa1c]     3:     <div id="error_explanation">
2016-12-12T06:40:48.173955+00:00 app[web.1]: F, [2016-12-12T06:40:48.173902 #4] FATAL -- : [f6758f60-0e48-4a7f-8d4d-cafca412aa1c]     4:       <div class="alert alert-danger">
2016-12-12T06:40:48.174113+00:00 app[web.1]: F, [2016-12-12T06:40:48.174055 #4] FATAL -- : [f6758f60-0e48-4a7f-8d4d-cafca412aa1c]   
2016-12-12T06:40:48.174062+00:00 app[web.1]: F, [2016-12-12T06:40:48.173950 #4] FATAL -- : [f6758f60-0e48-4a7f-8d4d-cafca412aa1c]     5:         The form contains <%= pluralize(object.errors.count, "error") %>.
2016-12-12T06:40:48.174178+00:00 app[web.1]: F, [2016-12-12T06:40:48.174107 #4] FATAL -- : [f6758f60-0e48-4a7f-8d4d-cafca412aa1c] app/views/shared/_error_messages.html.erb:2:in `_app_views_shared__error_messages_html_erb___3561868024025432384_70005941294020'
2016-12-12T06:40:48.174227+00:00 app[web.1]: F, [2016-12-12T06:40:48.174169 #4] FATAL -- : [f6758f60-0e48-4a7f-8d4d-cafca412aa1c] app/views/password_resets/edit.html.erb:7:in `block in _app_views_password_resets_edit_html_erb__4430889593317911195_70005941457600'
2016-12-12T06:40:48.174277+00:00 app[web.1]: F, [2016-12-12T06:40:48.174221 #4] FATAL -- : [f6758f60-0e48-4a7f-8d4d-cafca412aa1c] app/views/password_resets/edit.html.erb:6:in `_app_views_password_resets_edit_html_erb__4430889593317911195_70005941457600'

The app clearly is unable to display the password reset form. But I don't know why.

here is the edit form code: located in app/views/password_resets/edit.html.erb :

<% provide(:title, 'Reset password') %>
<h1>Reset password</h1>

<div class="row">
  <div class="col-xs-12 col-md-6 col-md-offset-3 col-sm-4 col-sm-offset-4">
    <%= form_for(@user, url: password_reset_path(params[:id])) do |f| %>
      <%= render 'shared/error_messages' %>

      <%= hidden_field_tag :email, @user.email %>

      <%= f.label :password %>
      <%= f.password_field :password, class: 'form-control' %>

      <%= f.label :password_confirmation, "Confirmation" %>
      <%= f.password_field :password_confirmation, class: 'form-control' %>

      <%= f.submit "Update password", class: "btn btn-primary" %>
    <% end %>
  </div>
</div>

and here is the controller code, located in app/controllers/password_resets_controller.rb :

class AccountActivationsController < ApplicationController

  def edit
    user = User.find_by(email: params[:email])
    if user && !user.activated? && user.authenticated?(:activation, params[:id])
      user.activate
      log_in user
      flash[:success] = "Account activated!"
      redirect_to user
    else
      flash[:danger] = "Invalid activation link"
      redirect_to root_url
    end
  end

end

Any pointer or suggestion will be much appreciated as I'm COMPLETELY stuck! thanks

Upvotes: 0

Views: 56

Answers (2)

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

Pass object to the partial

<%= render 'shared/error_messages', object: @user %>

Hope that helps!

Upvotes: 1

dp7
dp7

Reputation: 6749

In your app/views/password_resets/edit.html.erb:

Change

<%= render 'shared/error_messages' %>

to

<%= render partial: 'shared/error_messages', locals: {object: @user} %>

Upvotes: 2

Related Questions