2

I'm currently working through Chapter 8 of Michael Hartl's Ruby on Rails tutorial.

In section 8.2.1 The log_in method I'm getting an NoMethodError in SessionsController#create when calling log_in user. It's almost identical to this linked issue but I'm still stuck and I can't see why.

My Current Code


class SessionsController < ApplicationController
  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      log_in user
      redirect_to user
    else
      flash.now[:danger] = 'Invalid email/password combination'
      render 'new'
    end
  end
 end

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  include SessionsHelper
end

module SessionsHelper
  def log_in(user)
    session[:user_id] = user.id
  end
end

I can get it to work if I move log_in into SessionsController but I can't figure out why the controller can't use the method when it's in SessionsHelper. I've restarted my rails server and made sure everything is saved. I must of overlooked something but I can't see it myself.

Any ideas?


Edit:

Full Trace Stack

Started POST "/login" for ....
  ActiveRecord::SchemaMigration Load (0.2ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by SessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"D/F/6LjqRPOF3FGVqUxq8VheYVqtqPb3ozAtRUkj2O5jBrb4IVfCPFl3FH2nsMGs4h6hzyC5EuUgevUqGfYheA==", "session"=>{"email"=>"gmichael@example.com", "password"=>"[FILTERED]"}, "commit"=>"Log in"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."email" = ? LIMIT ?  [["email", "gmichael@example.com"], ["LIMIT", 1]]
#<NoMethodError: undefined method `log_in' for #<SessionsController:0x000000033fe848>
Did you mean?  login_url>
/home/ubuntu/workspace/sample_app/app/controllers/sessions_controller.rb:10:in `create'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/abstract_controller/base.rb:188:in `process_action'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_controller/metal/rendering.rb:30:in `process_action'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/abstract_controller/callbacks.rb:20:in `block in process_action'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:126:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:455:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:101:in `__run_callbacks__'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:750:in `_run_process_action_callbacks'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:90:in `run_callbacks'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/abstract_controller/callbacks.rb:19:in `process_action'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_controller/metal/rescue.rb:20:in `process_action'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.1/lib/active_support/notifications.rb:164:in `block in instrument'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.1/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.1/lib/active_support/notifications.rb:164:in `instrument'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_controller/metal/params_wrapper.rb:248:in `process_action'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.0.1/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/abstract_controller/base.rb:126:in `process'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionview-5.0.1/lib/action_view/rendering.rb:30:in `process'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_controller/metal.rb:190:in `dispatch'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_controller/metal.rb:262:in `dispatch'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/routing/route_set.rb:32:in `serve'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/journey/router.rb:39:in `block in serve'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/journey/router.rb:26:in `each'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/journey/router.rb:26:in `serve'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/routing/route_set.rb:725:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/rack-2.0.1/lib/rack/etag.rb:25:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/rack-2.0.1/lib/rack/conditional_get.rb:38:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/rack-2.0.1/lib/rack/head.rb:12:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/rack-2.0.1/lib/rack/session/abstract/id.rb:222:in `context'
/usr/local/rvm/gems/ruby-2.3.0/gems/rack-2.0.1/lib/rack/session/abstract/id.rb:216:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/cookies.rb:613:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.0.1/lib/active_record/migration.rb:553:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:97:in `__run_callbacks__'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:90:in `run_callbacks'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/callbacks.rb:36:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/executor.rb:12:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/web-console-3.1.1/lib/web_console/middleware.rb:131:in `call_app'
/usr/local/rvm/gems/ruby-2.3.0/gems/web-console-3.1.1/lib/web_console/middleware.rb:20:in `block in call'
/usr/local/rvm/gems/ruby-2.3.0/gems/web-console-3.1.1/lib/web_console/middleware.rb:18:in `catch'
/usr/local/rvm/gems/ruby-2.3.0/gems/web-console-3.1.1/lib/web_console/middleware.rb:18:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-5.0.1/lib/rails/rack/logger.rb:36:in `call_app'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-5.0.1/lib/rails/rack/logger.rb:24:in `block in call'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.1/lib/active_support/tagged_logging.rb:69:in `block in tagged'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.1/lib/active_support/tagged_logging.rb:26:in `tagged'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.1/lib/active_support/tagged_logging.rb:69:in `tagged'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-5.0.1/lib/rails/rack/logger.rb:24:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/sprockets-rails-3.2.0/lib/sprockets/rails/quiet_assets.rb:13:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/request_id.rb:24:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/rack-2.0.1/lib/rack/method_override.rb:22:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/rack-2.0.1/lib/rack/runtime.rb:22:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.1/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/executor.rb:12:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/static.rb:136:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/rack-2.0.1/lib/rack/sendfile.rb:111:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-5.0.1/lib/rails/engine.rb:522:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/puma-3.4.0/lib/puma/configuration.rb:224:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/puma-3.4.0/lib/puma/server.rb:569:in `handle_request'
/usr/local/rvm/gems/ruby-2.3.0/gems/puma-3.4.0/lib/puma/server.rb:406:in `process_client'
/usr/local/rvm/gems/ruby-2.3.0/gems/puma-3.4.0/lib/puma/server.rb:271:in `block in run'
/usr/local/rvm/gems/ruby-2.3.0/gems/puma-3.4.0/lib/puma/thread_pool.rb:114:in `block in spawn_thread'
No template found for SessionsController#create, rendering head :no_content
Completed 204 No Content in 155ms (ActiveRecord: 1.0ms)
Community
  • 1
  • 1
Karl Morosiuk
  • 151
  • 3
  • 12

3 Answers3

1

I think you need to define your method as a helper method by doing

helper_method :log_in

More information: http://apidock.com/rails/AbstractController/Helpers/ClassMethods/helper_method

Coolness
  • 1,932
  • 13
  • 25
  • So I'm getting an undefined method for `helper_method` instead of the previous`NoMethodError` when I add `helper_method :log_in` to SessionsHelper - which might be promising. [link](http://stackoverflow.com/questions/3992659/in-rails-what-exactly-do-helper-and-helper-method-do#3993323) suggests that `include SessionsHelper` which is in the `ApplicationController` should be working though and I can't see why not. – Karl Morosiuk Jan 08 '17 at 13:27
  • Hey @GetReQ, could you try putting your helper_method call into `ApplicationController` instead? I think it's defined only there. Also, where are you storing your `SessionsHelper`? If it's in the `lib` directory or someplace where Rails doesn't look for by default, you need to make sure you are [autoloading them](http://stackoverflow.com/questions/19098663/auto-loading-lib-files-in-rails-4) – Coolness Jan 08 '17 at 14:51
  • If I move it to `ApplicationController` I get the same results; only difference is if it's in `SessionsHelper` where I get an error thrown on the `helper_method` instead. I originally generated my `Session` object from the `rails` command so `sessions_helper.rb` is located in `app\controllers\helpers`. – Karl Morosiuk Jan 08 '17 at 20:57
  • Could you post the exact error you get, with the stacktrace? – Coolness Jan 08 '17 at 23:47
  • I've added the stacktrace to the edit above. I am new to Ruby on Rails so apologies if that's the information you're looking for. – Karl Morosiuk Jan 09 '17 at 09:59
0

In my case this issue is solved by removing [:session] and my login url was different like login_url instead of log_in

Here is solution

def create
user = User.find_by(email: params[:email])
if user && user.authenticate(params[:password])
  login_url user
  redirect_to user
else
  flash[:danger] = 'Invalid email/password combination' # Not quite right!
  render 'new'
end end

Hope it works

Bharat Mane
  • 55
  • 10
0

You can access helpers in Controllers by calling helpers. In your case:

helpers.log_in user

This may be an antipattern.

I found the information here: https://www.rubyguides.com/2020/01/rails-helpers/

jstn
  • 1