How can Rails automatically rescue from ActiveRecord::RecordNotFound in development mode? - ruby-on-rails-3

I am using the following piece of code on my ApplicationController :
rescue_from ActiveRecord::RecordNotFound, :with => :not_found
def not_found
render :nothing => true, :status => :not_found
end
in order make Rails respond with correct status code and not raise an exception
in development mode.
I know that how-to-handle-errors-like-404-500-in-rails3 explains how Rails works in production mode. In other words, what I am trying to do in development mode is done without any piece of code in production mode.
How can I make development mode behave like production mode and get rid of the above piece of code?
Note that I am using Rails 3.2.3

Perhaps you want to turn this off?:
config.consider_all_requests_local = false
This is set to true in config/environments/development.rb by default.

Related

How do I configure Capybara to work with Poltergeist?

I have an RSpec integration test that needs to execute some JavaScript. I've included Poltergeist and installed PhantomJS, but whenever I run the example, I get this error:
Failure/Error: page.execute_script("$('form')[0].submit();")
Capybara::NotSupportedByDriverError:
Capybara::Driver::Base#execute_script
The spec is:
require 'spec_helper'
describe "Signup", :type => :feature do
describe "workflow" do
it "ensures entry of contact information" do
visit 'signup/action'
# snip - use Capybara to fill out form elements
page.execute_script("$('form')[0].submit();")
page.should have_content("Name can't be blank")
page.should have_content("Email can't be blank")
# snip - use Capybara to fill out more form elements
page.execute_script("$('form')[0].submit();")
page.should have_content("Next page")
end
end
end
I think the problem is that I'm not sure how to indicate that Capybara should use Poltergeist as its JavaScript driver. The Poltergeist documentation says:
Installation
Add poltergeist to your Gemfile, and in your test setup add:
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist
But it doesn't say which file specifically it should go into. It also says:
Customization
You can customize the way that Capybara sets up Poltegeist via the following code in your test setup:
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, options)
end
But it's not clear to me if or when I would need to include this in my tests. And again, I'm not sure where to put it if I need to.
What am I missing?
Where do I need to put the configuration for Capybara and Poltergiest, and what exactly does it need to say (or how can I determine that for myself)?
Is there a step or piece of configuration that I missed?
Try putting js: true in your describe line. I know i had to do that for feature specs on an app at work:
describe "Signup", :type => :feature, :js => true do
I don't see any other configuration for it. Was a while ago when I set it up :)
You can just call the Capybara driver config methods once before your RSpec.configure block:
Capybara.default_selector = :css
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, :window_size => [1920, 1080], :phantomjs_logger => nil)
end
Capybara.javascript_driver = :poltergeist
RSpec.configure do |config|
Also be sure to use truncation not transaction with database cleaner. Poltergeist runs on a separate thread, so you'll likely have weird db issues if you use transactional.
Edit
Ah the js true thing is mentioned under here: https://github.com/jnicklas/capybara#using-capybara-with-rspec in the capybara readme.

Guard-rails not providing REPL for binding.pry

I'm using guard-rails to run my rails server, my problem is I can't access the REPL when I add binding.pry I just get
From: /home/martinr/code/app/controllers/tools_controller.rb # line 2 ToolsController#index:
2: def index
=> 3: binding.pry
4: #end_date = Date.today.to_s
5: #start_date = Date.today.months_ago(3).to_s
7: end
[1] pry(#<ToolsController>)>
No REPL, how do I use pry with guard rails?
My Gemfile file looks like this
group :development, :test do
gem 'pry-rails' # for better console debugging
gem 'pry-debugger'
gem 'rb-inotify'
gem 'sqlite3'
end
My Guardfile:
guard 'rails', :debugger => true do
watch('Gemfile.lock')
watch(%r{^(config|lib)/.*})
end
I've set up my rails environment with Guard and Spork and I find that binding-pry acts strangely with guard. If I insert binding.pry into the code and then guard restarts my tests, there's no interactive debugging. But if I exit and start guard up again, it's working and breaks into interactive mode correctly.
However... if I then remove the binding.pry line, guard will rerun the tests as it is supposed to, but will break at where the binding line used to be, even though it isn't there anymore.
Seems you have to restart guard every time you insert or remove the pry binding.
Irritating but still better than not having access to pry in your tests.
I'm trying a similar thing, and also can't get it to work. The issue seems that reading from stdin does not block, so Pry does not block. Any read from STDIN returns right away.
rspec -X console.rb
File follows:
require 'spec_helper'
describe 'console' do
it 'opens!' do
Pry.config.input = STDIN
Pry.config.output = STDOUT
puts STDIN.closed? # returns false
binding.pry # returns right away, does not block
gets # returns right way, does not block
end
end

ruby-ldap gem not work in rails3 app, but work in rails console

I want to build a rails3 website authed with LDAP, so I chose ruby-ldap gem (not net/ldap) which we used in our old rails2 apps and works very well.
But I keep on getting weird error in rails3 app, See the codes below:
require 'ldap'
class WelcomeController < ApplicationController
def index
begin
#test = LDAP::Conn.new('10.72.64.11', 389)
rescue LDAP::Error
p LDAP::Error
end
render :text => "ok"
end
end
welcome#index is my root route. Most time, the app crashes when going to LDAP::Conn.new('10.72.64.11', 389), even I tried to use "pry" to debug and track, throwing
[1] 24797 trace trap rails s
and the WEBrick server will be terminated right that time.
Sometimes it throws another type error when I use "pry" to step,
#<NameError: uninitialized constant WelcomeController::LDAP>
While try it in the console, everything goes well.
1.9.3-p194 :001 > require 'ldap'
=> true
1.9.3-p194 :002 > #test = LDAP::Conn.new('10.72.64.11', 389)
=> #<LDAP::Conn:0x00000101289568>
1.9.3-p194 :003 >
Can you guide me out of this crazy stuff? I am using ruby 1.9.3p194 and rails 3.2.8
A few months later, I kind of figure out what the problem is...
The ruby-ldap gem has problem on running on the rails default server: Webrick.
Try Pow or Passenger, it works perfect!
After reading this page: http://www.ruby-forum.com/topic/62920
I tried moving the require 'ldap' from the controller or model file, and into the very top line of my environment file (xxxlocal.rb)
After I did that, I was able to run it ok in webrick also.

Intermittent InvalidSalt exception when running rspec tests with devise

Since upgrading to Ruby 1.9.2 (Rails 3.0.3), our RSpce (2.7.0) tests using FactoryGirl (2.3.2) fail due to BCrypt::Errors::InvalidSalt upon creating of user fixture. Here is what we use for creating fixtures:
Factory.define :user, :class => User do |u|
u.email 'an#other.com'
u.api_key '123456'
u.password 'password'
end
It used to work fine, but now tests that have the line user = Factory.create(:user) fail randomly with different tests failing in each run.
Any ideas?

Getting NoMethodError (undefined method `name' for nil:NilClass) when creating a new model in Heroku console

I just did a push to Heroku and tried doing some testing by adding a model through rails_admin. When I did that I got a generic error page. I went into the logs and noticed this message:
NoMethodError (undefined method `name' for nil:NilClass)
I then opened heroku console and tried adding the model manually and received the same message when trying to save.
NoMethodError: undefined method `name' for nil:NilClass
Here is the model:
class Board < ActiveRecord::Base
attr_accessible :name, :description
validates :name, :presence => true
validates :description, :presence => true
validates_uniqueness_of :name, :case_sensitive => false
has_many :subjects
scope :hidden, where(:is_hidden => true)
scope :visible, where(:is_hidden => false)
end
Any ideas what might be happening with this - or where to start looking?
I did the migration and was able to see that it recognized the model and it's attributes when working in the console.
Thanks!
After being bitten by this issue twice, I asked heroku support team for the reason why it happens. And they replied:
"after running rake db:migrate, you have to restart your application so it can pick up the schema changes, since the schema info is cached during boot in production mode."
So always remember to restart heroku app by 'heroku restart' after running a new migration.
I'm not sure if there is a delay or something with Heroku when pushing changes out and running db:migrate but after spending a few hours out and about, I came back, ran rake db:migrate again, which appeared to do nothing and then I tried creating the model again and it worked without any problems.
So all seems well now, but I can't tell I just needed to wait longer before testing with Heroku - or whether running the migration again actually did something.