Monday, November 30, 2015

Sunday, November 29, 2015

Could not find 'railties' (>= 0) among 46 total gems

This happened in a Rails 5 alpha installation. Use:

bundle exec rails -v
bundle exec rails c

to resolve this issue.

Thursday, November 26, 2015

Rails 5 Quickly Book

I am updating my Rails 4.2 Quickly book to Rails 5. If you want to read it online, here are the links:

Chapter 1 : Running the Server
Chapter 2 : Hello Rails
Chapter 3 : Model
Chapter 4 : Model View Controller
Chapter 5 : View to Model
Chapter 6 : Update Article
Chapter 7 : Show Article
Chapter 8 : Delete Article
Chapter 9 : View Duplication
Chapter 10 : Relationships
Chapter 11 : Delete Comment
Chapter 12 : Restricting Operations

For initial setup, checkout this article : Creating a Rails 5 Project

Tuesday, November 24, 2015

JSON::ParserError:

JSON::ParserError:
       757: unexpected token at

Solution:

Escape the double quotes:
"{\"foo\":\"bar\"}"

`class_exec': no block given (LocalJumpError)

This happens in Rspec if you miss the it() method. For example:

describe Car
   expect(Car.speed).to eq(0)
end

The error message is not beginner friendly and can be improved.

Thursday, November 19, 2015

Rename all .css.scss to .scss file

Stolen script to make upgrade to Rails 4.2 easier: Save it as .sh file, run chmod 755 renamer.sh

#! /usr/bin/env bash


for f in $(find . -type f -iname '*.css.scss'); do

renamed=$(echo "${f}" | sed 's/.css.scss$/.scss/g')

cmd="git mv ${f} ${renamed}"

echo $cmd

eval $cmd

done

  

Monday, November 16, 2015

Wait for External calls to finish

Note to myself: Create an utility similar to this : https://robots.thoughtbot.com/automatically-wait-for-ajax-with-capybara to fix the sleep hack in Stripe project.

# spec/support/wait_for_external_call.rb
module WaitForExternalCall
  def wait_for_remote_call
    Timeout.timeout(Capybara.default_wait_time) do
      loop until custom_assertion_passed?
    end
  end

  def custom_assertion_passed?
   
  end
end

RSpec.configure do |config|
  config.include WaitForExternalCall, type: :feature
end

How to set default editor for bundle open

 export BUNDLER_EDITOR='mate'

Sunday, November 15, 2015

Testing Tip

Instead of checking each attribute of a JSON response in your test, you can use json-schema for api validation. Use jsonschema.net to generate a valid JSON schema from a valid sample data. You can delete all the links that point to jsonschema.net and save the json file in the fixtures directory.

Could not find 'railties' (>= 0) among 46 total gem(s) (Gem::LoadError) in Rails 5 Project

Problem

zepho-mac-pro:rails5 zepho$ rails -v
/Users/zepho/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rubygems/dependency.rb:315:in `to_specs': Could not find 'railties' (>= 0) among 46 total gem(s) (Gem::LoadError)
Checked in 'GEM_PATH=/Users/zepho/.rvm/gems/ruby-2.2.3@r5blog:/Users/zepho/.rvm/gems/ruby-2.2.3@global', execute `gem env` for more information
from /Users/zepho/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rubygems/dependency.rb:324:in `to_spec'
from /Users/zepho/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_gem.rb:64:in `gem'
from /Users/zepho/.rvm/gems/ruby-2.2.3@r5blog/bin/rails:22:in `
'


Fix:

zepho-mac-pro:rails5 zepho$ bundle check
Resolving dependencies...
The Gemfile's dependencies are satisfied
zepho-mac-pro:rails5 zepho$ gem list rails

*** LOCAL GEMS ***

jquery-rails (4.0.5)
rails-deprecated_sanitizer (1.0.3)
rails-dom-testing (1.0.7)
rails-html-sanitizer (1.0.2)
sprockets-rails (2.3.3)
zepho-mac-pro:rails5 zepho$ gem list railties

*** LOCAL GEMS ***


zepho-mac-pro:rails5 zepho$ gem install rails
Fetching: rack-1.6.4.gem (100%)
Successfully installed rack-1.6.4
Fetching: activesupport-4.2.5.gem (100%)
Successfully installed activesupport-4.2.5
Fetching: actionview-4.2.5.gem (100%)
Successfully installed actionview-4.2.5
Fetching: actionpack-4.2.5.gem (100%)
Successfully installed actionpack-4.2.5
Fetching: railties-4.2.5.gem (100%)
Successfully installed railties-4.2.5
Fetching: activejob-4.2.5.gem (100%)
Successfully installed activejob-4.2.5
Fetching: actionmailer-4.2.5.gem (100%)
Successfully installed actionmailer-4.2.5
Fetching: arel-6.0.3.gem (100%)
Successfully installed arel-6.0.3
Fetching: activemodel-4.2.5.gem (100%)
Successfully installed activemodel-4.2.5
Fetching: activerecord-4.2.5.gem (100%)
Successfully installed activerecord-4.2.5
Fetching: rails-4.2.5.gem (100%)
Successfully installed rails-4.2.5
11 gems installed
zepho-mac-pro:rails5 zepho$ rails -v
Rails 5.0.0.alpha

Creating a Rails 5 Project

1. Create a Gemfile

source "https://rubygems.org"

ruby '2.2.3'

gem 'rack', github: 'rack/rack'
gem 'rails', git: 'git://github.com/rails/rails.git'
gem 'arel', git: 'git://github.com/rails/arel.git'

2. bundle

3. bundle exec rails new . --dev --force

4. bundle exec rails s

Go to localhost:3000, you should now see 5.0.0.alpha in the environment.

Reference

Setting up Rails 5 app from edge

Monday, November 09, 2015

ActionController::UnknownHttpMethod:

In Rails 4.x:

In your tests, call process method like this:

 process :some_action, 'OPTIONS'

Sunday, November 08, 2015

Redefining a Private Method in Ruby

class ActiveRecord

  private

  def hi
    'I am private'
  end
end

class ActiveRecord

  def greet
    hi
  end


  private

  def hi
    'I am redining you'
  end

end

a = ActiveRecord.new

p a.greet

Monkey patching (Coding Horror):

class ActiveRecord

  private

  def hi
    'I am private'
  end
end

class MyActiveRecord < ActiveRecord

  def greet
    hi
  end


  private

  def hi
    'I am redefining you'
  end

end

a = MyActiveRecord.new

p a.greet

Unnecessary Complication:

class Client

  def print
    say
  end

  private
 
  def say
    "hello"
  end
end

# Create a subclass of Client
MyClient = Class.new(Client) do
  define_method(:say) do
    "hello from an overridden private method"
  end
end

puts MyClient.superclass

my_client = MyClient.new
puts my_client.print

Simpler Way to Accomplish the Same Thing:

class Client

  def print
    say
  end

  private
 
  def say
    "hello"
  end
end

class MyClient < Client
  def say
    "hello from an overridden private method"
  end
end

puts MyClient.superclass

my_client = MyClient.new
puts my_client.print





Thursday, November 05, 2015

Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true rails 4.2.4

In application.rb, inside the class add:

Rails.application.routes.default_url_options[:host] =  "load url that is appropriate for different environments in your app here"

Now you can go to the rails console:

 > include Rails.application.routes.url_helpers

and call different url_helpers like this:

profile_url