Rails Tutorial Chapter 8 - undefined local variable or method `signin_path' - railstutorial.org

I'm getting the following rSpec failed tests error.
What did I do wrong?
Many thanks..
1) Authentication signin page Failure/Error: before { visit signin_path } NameError: undefined local variable or method signin_path' for # # ./spec/requests/authentication_pages_spec.rb:8:inblock (3 levels) in '
2) Authentication signin page Failure/Error: before { visit signin_path } NameError: undefined local variable or method signin_path' for # # ./spec/requests/authentication_pages_spec.rb:8:inblock (3 levels) in '
session_controller.rb:
class SessionsController < ApplicationController
def
new
end
def
create
end
def
destroy
end
end
routes.rb:
SampleApp::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
root to: 'static_pages#home'
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
authentication_pages_spec.rb:
require 'spec_helper'
describe "Authentication" do
subject { page }
describe "signin page" do before { visit signin_path }
it { should have_selector('h1', text: 'Sign in') }
it { should have_selector('title', text: 'Sign in') }
end
end

I had this same issue and restarting guard/spork after I created my routes solved the problem

Did you add the signin route to routes.rb and not restart spork (assuming you're using it) before running your tests?

Can you run rake routes from your console and show the output

Related

Ruby on Rails Tutorial NoMethodError in Users#index

I'm reading Michael Hartl's great "The Ruby on Rails Tutorial". And I faced a problem that wasn't described in the book.
I push a submit button on a Sign up page with blank fields, then, as it expected, the mistakes messages like these appears:
Name can't be blank
Email can't be blank
Email is invalid
Password can't be blank
But the URL changes from http://[::1]:3000/signup to http://[::1]:3000/users
Then I push the Chrome's refresh button and get redirecting to exactly http://[::1]:3000/users URL.
I understand that the pushing the submit button directs to .../users URL. How to change this route to the right one (still http://[::1]:3000/signup)?
Here's my users_controller.rb file
class UsersController < ApplicationController
before_action :logged_in_user, only: [:show, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy
def index
#users = User.where(activated: true).paginate(page: params[:page])
end
def show
#user = User.find(params[:id])
redirect_to root_url and return unless #user.activated?
#microposts = #user.microposts.paginate(page: params[:page])
end
def new
#user = User.new
end
def create
#user = User.new(user_params)
if #user.save
#user.send_activation_email
flash[:info] = "Please check your email to activate your account."
redirect_to root_url
else
render 'users/new', status: :unprocessable_entity
end
end
def edit
#user = User.find(params[:id])
end
end
Here's my routes.rb file
Rails.application.routes.draw do
get 'password_resets/new'
get 'password_resets/edit'
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
default_url_options :host => "example.com"
resources :users
end
Thanks in advance!

Elixir issue with Bamboo.SentEmailViewerPlug

I do have an issue with mentioned function. This is the error from browser:
function Bamboo.SentEmailViewerPlug.init/1 is undefined (module Bamboo.SentEmailViewerPlug is not available)
And this is from console :
[error] #PID<0.868.0> running RewardappWeb.Endpoint (connection #PID<0.829.0>, stream id 4) terminated
Server: localhost:4000 (http)
Request: GET /mailbox
** (exit) an exception was raised:
** (UndefinedFunctionError) function Bamboo.SentEmailViewerPlug.init/1 is undefined (module Bamboo.SentEmailViewerPlug is not available)
Bamboo.SentEmailViewerPlug.init([])
(phoenix 1.6.6) lib/phoenix/router/route.ex:41: Phoenix.Router.Route.call/2
(phoenix 1.6.6) lib/phoenix/router.ex:355: Phoenix.Router.__call__/2
(rewardapp 0.1.0) lib/rewardapp_web/endpoint.ex:1: RewardappWeb.Endpoint.plug_builder_call/2
(rewardapp 0.1.0) lib/plug/debugger.ex:136: RewardappWeb.Endpoint."call (overridable 3)"/2
(rewardapp 0.1.0) lib/rewardapp_web/endpoint.ex:1: RewardappWeb.Endpoint.call/2
(phoenix 1.6.6) lib/phoenix/endpoint/cowboy2_handler.ex:54: Phoenix.Endpoint.Cowboy2Handler.init/4
(cowboy 2.9.0) /Users/mateuszosinski/Desktop/elixir/rewardapp/deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2
(cowboy 2.9.0) /Users/mateuszosinski/Desktop/elixir/rewardapp/deps/cowboy/src/cowboy_stream_h.erl:306: :cowboy_stream_h.execute/3
(cowboy 2.9.0) /Users/mateuszosinski/Desktop/elixir/rewardapp/deps/cowboy/src/cowboy_stream_h.erl:295: :cowboy_stream_h.request_process/3
(stdlib 3.17.1) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Here is my router.ex file :
defmodule RewardappWeb.Router do
use RewardappWeb, :router
use Phoenix.Router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_live_flash
plug :put_root_layout, {RewardappWeb.LayoutView, :root}
plug :protect_from_forgery
plug :put_secure_browser_headers
end
pipeline :api do
plug :accepts, ["json"]
end
scope "/", RewardappWeb do
pipe_through :browser
#get "/", PageController, :index
#NEW ROUTES
#get "/users", GrantController, :login
get "/admin", GrantController, :admin
get "/main", GrantController, :main
get "/", GrantController, :index
post "/", GrantController, :login
get "/add", GrantController, :add
post "/add/:id", GrantController, :update
get "/admin/delete/:id", GrantController, :delete
end
if Mix.env == :dev do
forward "/mailbox", Bamboo.SentEmailViewerPlug
end
# Other scopes may use custom stacks.
# scope "/api", RewardappWeb do
# pipe_through :api
# end
# Enables LiveDashboard only for development
#
# If you want to use the LiveDashboard in production, you should put
# it behind authentication and allow only admins to access it.
# If your application does not have an admins-only section yet,
# you can use Plug.BasicAuth to set up some basic authentication
# as long as you are also using SSL (which you should anyway).
if Mix.env() in [:dev, :test] do
import Phoenix.LiveDashboard.Router
scope "/" do
pipe_through :browser
live_dashboard "/dashboard", metrics: RewardappWeb.Telemetry
end
end
# Enables the Swoosh mailbox preview in development.
#
# Note that preview only shows emails that were sent by the same
# node running the Phoenix server.
if Mix.env() == :dev do
scope "/dev" do
pipe_through :browser
#forward "/mailbox", Plug.Swoosh.MailboxPreview
end
end
end
Mails are being sent totally fine - I have inspected that with IO.inspect, and right now, they are sent fine. However, I can not attempt to see localhost:4000/mailbox. Where is my mistake? Thank you in advance!!
The issue was with wrong name Bamboo module name.

RubyMine shows alert 'Cannot find "before_action" '

RubyMine7.0.1 shows this alert in every controller class.
A controller class file below.
Thanks.
application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
helper_method :current_user, :logged_in?
before_action :authenticate
private
def current_user
return unless sessions[:user_id]
#current_user ||= User.find(session[:user_id])
end
def logged_in?
!!session[:user_id]
end
def authenticate
return if logged_in?
redirect_to root_path, alert: 'ログインして下さい。'
end
end
I got the very same error when I imported a project from existing sources and the Project SDK was not set properly for the project.

RailsTutorial: NoMethodError 'permanent' Rake::Test::CookieJar

app/helpers/sessions_helper.rb
module SessionsHelper
def sign_in(user)
cookies.permanent[:remember_token] = user.remember_token
self.current_user = user
end
def sign_out
self.current_user = nil
cookies.delete(:remember_token)
end
def signed_in?
!current_user.nil?
end
def current_user=(user)
#current_user = user
end
def current_user
#current_user ||= User.find_by_remember_token(cookies[:remember_token])
end
end
Tests defined in section 9.2.1 Requiring signed-in users are failing:-
At first I was getting sign_in method not found then I added
include SessionsHelper
in spec/utilities.rb file after that I started getting below error, saying no method with name permanent exists in Rake::Test::CookieJar.
Is it due to some Gem version issue.
1) User Pages edit page
Failure/Error: before { sign_in user}
NoMethodError:
undefined method `permanent' for #<Rack::Test::CookieJar:0x007ff12c661e88>
# ./app/helpers/sessions_helper.rb:3:in `sign_in'
# ./spec/requests/user_pages_spec.rb:55:in `block (3 levels) in <top (required)>'
Just ran into the same problem and got it fixed.
It seems they are not referring to the sign_in method in sessions_helper.rb but the the sign_in method in spec/support/utilities.rb
In my case this helper method in utilities.rb had a different name, after renaming it everything worked fine :-)

Setting env when using rspec to test omniauth callbacks

I'm having a strange problem when trying to set a callback for Facebook Authentication via Omniauth. In my controller (simplified to just the code necessary to show the error) I have:
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
raise env.inspect
# auth_hash = env["omniauth.auth"]
end
end
this works in production mode, showing me the hash. However in test mode env is set to nil.
I have the following set in my spec_helper.rb file
OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:facebook, {"credentials" => {
"token" => "foo-token"
}
})
and my spec looks like this:
require 'spec_helper'
describe Users::OmniauthCallbacksController do
describe "Facebook" do
before(:each) do
request.env["devise.mapping"] = Devise.mappings[:user]
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]
end
it "should be a redirect" do
get :facebook
response.should redirect_to(root_path)
end
end
end
Can anyone enlighten me on what I need to do to have env not be nil when running my tests?
I use the following in my spec_helper.rb :
RACK_ENV = ENV['ENVIRONMENT'] ||= 'test'
I don't use Rails or Devise though so YMMV. I've also seen various threads saying that someone had to do this before their requires to get it to work.