Previously I talked about how to use save_and_open_page with Cucumber. Unfortunately, when you do this it renders an unstyled page. If you add
Capybara.asset_host = 'http://localhost:3000'
to your spec_helper.rb or Capybara config file, save_and_open_page will use your local server's assets - making it much easier to diagnose issues.
A blog about the problems I've encountered while being a Ruby on Rails developer, and how I've solved them.
Thursday, March 26, 2015
Monday, March 23, 2015
undefined method `capture_haml' for RSpec
If you're using Haml in a Rails project, testing with RSpec, you might encounter the "undefined method 'capture_haml' for ..." error while trying to run your specs. I encountered this error in a view spec when trying to use Simple Form along with Haml. Fortunately, like many other obscure bugs there is a quick and dirty solution:
The init_haml_helpers section is necessary to prevent other errors from happening.
Note that all lines are required. If you drop the
and of course if you don't have any of this, you will get:
Note that all lines are required. If you drop the
init_haml_helpers line, you'll get the following error:
ActionView::Template::Error:
undefined method `capture_position=' for nil:NilClass
and of course if you don't have any of this, you will get:
ActionView::Template::Error:
undefined method `capture_haml' for #<RSpec::ExampleGroups::...
Thursday, March 19, 2015
Using save_and_open_page with Cucumber
Capybara has a nice utility method called save_and_open_page that will save the current page's HTML as a temp file and open it in your browser. This method is available by default in Ruby code (for example in an RSpec feature spec). As Cucumber is not direct Ruby, you can't directly say that. Fortunately, you can say
and you'll get the same effect.
Then show me the page
and you'll get the same effect.
Wednesday, March 18, 2015
How to Validate Regular Expressions in Ruby on Rails Models
ActiveRecord has many awesome validators for common things like presence, numericality, etc. Unfortunately, it is missing one common format - regex, or regular expressions. These are normally saved as string fields and, if not validated, can lead to page errors when your app tries to use a field that contains an invalid regular expression. This is especially the case if you allow the user to input a regular expression. With the addition of one small validation class, you will be able to prevent these sorts of bugs with your models.
The above code adds a validator called RegularExpressionValidator to app/validators/regular_expression_validator.rb. It then adds the validation to the Bear model on the field my_regex_field. With this, if you try and create a Bear with an invalid regular expression, an error will be thrown and the save will not complete.
The above code adds a validator called RegularExpressionValidator to app/validators/regular_expression_validator.rb. It then adds the validation to the Bear model on the field my_regex_field. With this, if you try and create a Bear with an invalid regular expression, an error will be thrown and the save will not complete.
Monday, March 16, 2015
undefined method `fa_icon` for #<#Class:...
If you're trying to use the Font-Awesome gem in your Rails engine, you may run into this "undefined method `fa_icon for #<#Class:.." error. Fortunately, there is a pretty simple fix.
Add
to
and the error should go away.
Thanks to Github for the solution.
Add
include FontAwesome::Rails::IconHelper
to
engine_name/app/helpers/engine_name/application_helper.rb
and the error should go away.
Thanks to Github for the solution.
Thursday, March 12, 2015
How to Kill an "open" Rails Server
If you've force-closed your Rails server instance for whatever reason (like a bug that froze up the server), you might run into issues when you try and restart it. For example, you might get the error "WARN TCPServer Error: Address already in use - bind(2) Exiting" What do you do?
Assuming that you're running on port 3000, find the PID of the process with
lsof -wni tcp:3000
and kill the process with
kill -9 PID
Assuming that you're running on port 3000, find the PID of the process with
lsof -wni tcp:3000
and kill the process with
kill -9 PID
Monday, March 9, 2015
Testing with a Dummy Controller in a Rails Engine
If you have a controller in your dummy app (spec/dummy/app/controllers/dummy_controller.rb) for your Rails engine, you might need to access the list of routes within your spec code. You can do this, surprisingly, with Rails.application.routes.routes!
Subscribe to:
Posts (Atom)