RailsTutorial: NoMethodError 'permanent' Rake::Test::CookieJar - railstutorial.org

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 :-)

Related

How to setup factory_girl_rails with Rails 5.0.1

I just setup a new Rails project with Rails 5.0.1
I added factory_girl_rails to the development and test group in the Gemfile.
Then I created a article factory in the spec/factories folder:
FactoryGirl.define do
factory :article do
title { Faker::Lorem.sentence }
body { Faker::Lorem.paragraph }
end
end
my feature spec looks like this:
require 'rails_helper'
feature "guest views articles" do
let(:article1) { FactoryGirl.create(article) }
let(:article2) { FactoryGirl.create(article) }
scenario "by visiting index page" do
visit articles_path
expect(page).to have_content article1.title
expect(page).to have_content article2.title
end
end
However, when I run the spec, I get this error message:
Failures:
1) guest views articles by visiting index page
Failure/Error: let(:article1) { FactoryGirl.create(article) }
NameError:
undefined local variable or method `article' for # <RSpec::ExampleGroups::GuestViewsArticles:0x007fe957fad078>
Did you mean? article2
article1
# ./spec/features/guest_views_articles_spec.rb:5:in `block (2 levels) in <top (required)>'
# ./spec/features/guest_views_articles_spec.rb:10:in `block (2 levels) in <top (required)>'
from the error message, it seems like the article factory is not defined or recognized. Did I forgot something when setting up the article factory?
my repository is at:
https://github.com/acandael/personalsite/tree/first_test
thanks for your help,
Anthony
found the issue:
let(:article1) { FactoryGirl.create(article) }
has to be
let(:article1) { FactoryGirl.create(:article) }

CanCan singleton loading with pluralized class name

I'm currently experiencing an issue with the latest version of CanCan. It seems that the gem won't load singleton resources with pluralized names.
class Preferences < ActiveRecord::Base
self.table_name = 'preferences'
belongs_to :user
end
class User < ActiveRecord::Base
has_one :preferences
end
#controller
class PreferencesController < ApplicationController
before_filter :authenticate_user! #from Devise
load_and_authorize_resource :singleton => true,
:through => :current_user,
:through_association => :preferences,
:parent => false
end
#ability.rb
class Ability
include CanCan::Ability
def initialize(user)
can [:read, :update], Preferences, :user_id => user.id
end
end
I keep getting an error like this when I run my controller spec:
Failures:
1) PreferencesController GET #show should return http success
Failure/Error: get :show
NameError:
uninitialized constant Preference
# ./spec/controllers/preferences_controller_spec.rb:10:in `block (3 levels) in <top (required)>'
It seems no matter what combination of load_resource arguments I try, I can't get my specs to pass again without reverting back to loading the resource manually like this:
class PreferencesController < ApplicationController
before_filter :authenticate_user!
def show
#preferences = current_user.preferences
authorize! :read, #preferences
end
end
Is there some magic combination of load_and_authorize_resource parameters I need to use? I have this same configuration working in another singleton controller where the resource falls under normal Rails conventions (i.e. User has_one :profile), so I know this works already under normal situations.

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

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

Redirected to http://localhost:3000/session/new

So I have a rails app 2.x app that works fine via the web, but when trying to perform a POST I keep getting "Redirected to http://localhost:3000/session/new Filter chain halted as [:require_user] rendered_or_redirected.". In my iPhone app, I can create a new session and sign-in via my iPhone app, but cannot POST to say the POSTS_Controller.
I have this in my code
Posts_Controller
before_filter :require_user, :only => [:create, :update, :destroy]
Application_Controller
# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
class ApplicationController < ActionController::Base
include AuthenticatedSystem
include Geokit::Geocoders
helper :all # include all helpers, all the time
#session :session_key => '_cwa_session_id'
#filter_parameter_logging :password
# See ActionController::RequestForgeryProtection for details
# Uncomment the :secret if you're not using the cookie session store
protect_from_forgery # :secret => 'eejj7eded74769099999944a729b4f'
#filter_parameter_logging(:password)
before_filter :login_from_cookie
before_filter :find_user_interests
before_filter :find_user_posts
protected
def find_user_interests
#user_interests = cur_user ? cur_user.interesting_posts : []
logger.debug "User interests hash: #{current_user.inspect}"
end
def find_user_posts
#user_posts = cur_user ? cur_user.posts : []
end
def cur_user
User.find(session[:user_id]) if session[:user_id]
end
def require_user
unless cur_user
flash[:error] = "You must be logged in to do that."
redirect_to '/session/new'
return false
end
end
geocode_ip_address
def geokit
#location = session[:geo_location]
end
end
I have been working on this for 2 months and cannot figure out the issue. In my iPhone app I am using ObjectiveResource. I am sending over json and have "Mime::Type.register_alias "application/json", :json" set up on the rails side.
I am not a rails developer, but the before filter for require_user is unable to pass the cur_user test in that is cannot find :user_id in the session hash. Are you sure that you have a session that persists when using the iPhone? Are you using devise for authentication? Just for kicks, does it work if you manually pass the user_id as params?

MMS2R and Multiple Images Rails

Here's my code:
require 'mms2r'
class IncomingMailHandler < ActionMailer::Base
##
# Receives email(s) from MMS-Email or regular email and
# uploads that content the user's photos.
# TODO: Use beanstalkd for background queueing and processing.
def receive(email)
begin
mms = MMS2R::Media.new(email)
##
# Ok to find user by email as long as activate upon registration.
# Remember to make UI option that users can opt out of registration
# and either not send emails or send them to a username+32523#example.com
# type address.
##
# Remember to get SpamAssasin
if (#user = User.find_by_email(email.from) && email.has_attachments?)
mms.media.each do |key, value|
if key.include?('image')
value.each do |file|
#user.photos.push Photo.create!(:uploaded_data => File.open(file), :title => email.subject.empty? ? "Untitled" : email.subject)
end
end
end
end
ensure
mms.purge
end
end
end
and here's my error:
/usr/local/lib/ruby/gems/1.8/gems/rails-2.3.4/lib/commands/runner.rb:48: undefined method `photos' for true:TrueClass (NoMethodError)
from /usr/home/xxx/app/models/incoming_mail_handler.rb:23:in `each'
from /usr/home/xxx/app/models/incoming_mail_handler.rb:23:in `receive'
from /usr/home/xxx/app/models/incoming_mail_handler.rb:21:in `each'
from /usr/home/xxx/app/models/incoming_mail_handler.rb:21:in `receive'
from /usr/local/lib/ruby/gems/1.8/gems/actionmailer-2.3.4/lib/action_mailer/base.rb:419:in `receive'
from (eval):1
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `eval'
from /usr/local/lib/ruby/gems/1.8/gems/rails-2.3.4/lib/commands/runner.rb:48
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from /home/xxx/script/runner:3
I sent an email to the server with two image attachments. Upon receiving the email the server runs
"| ruby /xxx/script/runner 'IncomingMailHandler.receive STDIN.read'"
What is going on? What am I doing wrong?
(MMS2R docs)
Please replace
if (#user = User.find_by_email(email.from) && email.has_attachments?)
with
if ((#user = User.find_by_email(email.from)) && email.has_attachments?)