RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

I am getting tasks_count is marked as readonly while running migration. Fixed with solution given on SO

Avatar

Another great tutorial by Ryan!

MiniTest has evolved significantly since this tutorial was created. There are changes in configuration, and to help clarify the differences between the 3 main test frameworks I wrote a guide that examines their specific characteristics.

Here is a link to Ruby Reflections where you can find configuration for Rails 4.1 Test::Unit, MiniTest, Rspec, and combined with fixtures or factories. Pattern Objects, mocking, stubbing, and debugging hints are also covered.

Database cleaner is also explained, when it is necessary and when it is not.

Avatar

Hi otagi I get this error OpenURI::HTTPError 403 Forbidden

def inspections_rows
    [['Porciento','Inspección','Fecha','Gerente',{:content => "Cliente", :colspan => 2},'Cantidad de Áreas']] +
      @inspections.map do |inspection|
       [percentage(inspection.inspection_score),inspection.name, inspection.date,inspection.manager,inspection.client,
       image_cell(inspection.client_sign(:small).to_s.sub!(/\?.+\Z/,'')),inspection.inspection_sections.count]

    end
  end

  def image_cell(image_url)
    {image: open(image_url), fit: [IMAGE_SIZE, IMAGE_SIZE], position: :center}
  rescue SocketError
    ""
  end
Avatar

could u solved this problem? when i run '$irb' ,give me a message 'Unable to load pry', when i run '$require 'pry'' i got
"LoadError: no such file to load -- pry
from (irb):1:in `require'
from (irb):1
"

how could i fix it how could i use pry instead rib

Avatar

could u solved this problem? when i run '$irb' ,give me a message 'Unable to load pry', when i run '$require 'pry'' i got
"LoadError: no such file to load -- pry
from (irb):1:in `require'
from (irb):1
"

how could i fix it how could i use pry instead rib

Avatar

could u solved this problem? i didn't resolve, could u tell me, how can i do?

Avatar

Hey everybody. It's 2014, and as this episode is > 2 years old, a couple things have changed with the jquery-ui-rails gem:

app/assets/javascripts/application.js
//= require jquery-ui/datepicker
app/assets/stylesheets/application.css
*= require jquery-ui/datepicker

Source: https://github.com/joliss/jquery-ui-rails

I hope that helps someone... :)

Avatar

Working around unrelated objects can be tad tricky at times. Just the right info to get it done, fast.

Avatar

Hey. Thanks a lot for the timely info.

Avatar

Great work!

Really helped me a lot. Thanks.

Avatar

In rails 4, you now need to change the where() method parameters explicitly as hashes. This won't work

def self.from_omniauth(auth)
  where(auth.slice(:provider, :uid)).first_or_create do |user|
  # ...

You need to change it to

def self.from_omniauth(auth)
  where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
  # ...
Avatar

Hey Ryan,

Just wanted to mention you misspelled the gem name in the ASCIICast:

You put:

ruby
gem 'hrpicot'

It should be:

ruby
gem 'hpricot'

(Note the switch of hrp with hpr)

Great tutorial as always!

Avatar

Just a heads up for those in Rails 4, I got stuck because of 1 thing:

the "find_or_create_by" method has been deprecated in rails 4

so instead of this

ruby
def category_name=(name)
  self.category = Category.find_or_create_by_name(name) if name.present?
end

try this

ruby
def category_name=(name)
    self.category = Category.where(:name => name).first_or_create!
  end
end

source: http://stackoverflow.com/a/3046645/4195073

Hope that helps someone!

Avatar

I'm using this to insert an S3 image into a table cell:

ruby
class CustomPdf < Prawn::Document
  require 'open-uri'

  def image_cell(image_url)
    {image: open(image_url), fit: [IMAGE_SIZE, IMAGE_SIZE], position: :center}
  rescue SocketError
    ""
  end
end

Then just call image_cell in your table data array: [ [ image_cell(url), ... ], [ ... ] ].

Avatar

Don't touch rjs with a ten foot pole any more. It's completely deprecated and will break.

Avatar

have you found solution to this? can you send how you integrted it with active admin? thanks!

Avatar

Looking for a way to add to my Table a remote image from s3 to my prawn pdf.

Avatar

Stripe now available in the UK. And yes, this is by far the simplest payment system out there. Excellent cast as ever

Avatar

I was facing the same issue and this solved it. thanks David.

Avatar

I am trying to validate subdomain in Rails 4 but getting exception, can anyone plz help me ?

validates :subdomain, format: { with: /^[a-zA-Z0-9][a-zA-Z0-9.-]+[a-zA-Z0-9]$/ }

Error Message

ArgumentError at /

The provided regular expression is using multiline anchors (^ or $), which may present a security risk. Did you mean to use \A and \z, or forgot to add the :multiline => true option?
Avatar

You can get the dimension of the img using
img.columns, img.rows

I ended up doing something like this to prevent blurring of the cropped image:

ruby
def crop
  if model.crop_x.present?

    #resize_to_limit(300, 3000)

    manipulate! do |img|
      converted_h = 300.0*img.rows/img.columns
      x = (model.crop_x.to_i/300.0*img.columns).to_i
      y = (model.crop_y.to_i/converted_h*img.rows).to_i
      w = (model.crop_w.to_i/300.0*img.columns).to_i
      h = (model.crop_h.to_i/converted_h*img.rows).to_i
      #img.crop("#{w}x#{h}+#{x}+#{y}") 

      img.crop!(x, y, w, h)
      img
     end
  end
end

**Noticed this was from 2 years ago...

Avatar

This is late, but a tip for others...

Assuming the reason is for viewing it yourself in a browser, the easiest (and general fix) is to use a nice browser plugin. There are two plugins both named "JSONView" for Chrome and Firefox. I use the Chrome one. Very nice tool for viewing JSON API responses.

Avatar

Raygun is yet another option.

Does the exception_notification gem send notifications for rescued exceptions?

Edit: You have to explicitly send notifications for rescued exceptions.

Avatar

Really wish I had watched this a long time ago. Sorted out a bunch of muddled thoughts in my head. Superb

Avatar

Had to remove require iconv from config/application.rb to make this compile (with either ruby 1.9.2 or 2.0.0).

Most works fine, but when reading back the xsl file written with the example, I will get
OLE2 signature is invalid

Ok, I learned that this is a format problem (still think it's funny that I writes a format it cannot read).

So I open the xls file with libreoffice, and save it as 2007 xls.

Now I can read the file (no OLE problem anymore), but I get
Row 2: Price can't be blank
and so forth for every other line.

When opening the file from libreoffice, it has the prices in the price row.

Avatar

I have one question. if I want to have the autocomplete-source to return more than one attribute. how do i handle that?

Avatar

As the following:

where("firstname ilike :q or lastname ilike :q ", q: "%#{query}%")

Avatar

Thanks Ryan for this great episode. We are waiting for your comeback.

Avatar

@Solomon Thank you for posting this here. I've been looking for it for a while!

Avatar

when I tail the development log, I see this error message:

Started POST "/admin/sections" for 127.0.0.1 at 2014-11-13 20:49:51 +0100
Processing by Admin::SectionsController#create as /
Parameters: {"section"=>["3", "4", "6", "2", "5"]}
Completed 500 Internal Server Error in 1ms

NoMethodError (undefined method permit' for ["3", "4", "6", "2", "5"]:Array):
app/controllers/admin/sections_controller.rb:54:in
section_params'
app/controllers/admin/sections_controller.rb:15:in `create'

Avatar

I'm trying to implement sortable rows in my Rails 4 application. For some reason dragging a row doesn't save the position in the database. I'm not sure whether the sort action method gets called.
When I check the Javascript console with Google developer tools no Javascript errors are thrown, so I'm not sure what's the problem.

My code is at:
https://github.com/acandael/beautysalonapp2/tree/drag-and-drop

anyone else got issues implement this drag and drop functionality for a Rails 4 application?

thanks for your help,
Anthony

Avatar

I can't get it to work. Been trying for four days now... I'm so close with this tutorial, but I can't get it finished.

I try cap deploy:cold and get

failed: "sh -c 'cd /home/deployer/apps/arcane/releases/20141113053658 && bundle install --gemfile /home/deployer/apps/arcane/releases/20141113053658/Gemfile --path /home/deployer/apps/arcane/shared/bundle --deployment --quiet --without development test'" on 173.230.151.17

I'm using Capistrano 2.15, but I don't even know where the problem lies because theres so many different things... Any ideas?

Avatar

I would recommend to override the should_generate_new_friendly_id? method, because it will only change the slug when ONLY needs to do it.

class Post < ActiveRecord::Base
  extend FriendlyId
  friendly_id :title, :use => :slugged

  def should_generate_new_friendly_id?
    title_changed?
  end
end
Avatar

I'm not sure if heroku allows cron jobs or not but I had a similar issue with Passenger going to sleep after inactivity on a low traffic app. So in my crontab I added the following:

*/5 * * * * wget -O /dev/null -o /dev/null host.domain.com

This will issue a GET request every 5 minutes to your host and keep the app spun up. Note we output the results of wget to /dev/null to avoid any files being created on the server. Works like a charm.

Avatar

Works with :

raffle.js.coffee
angular.module('Raffler', []).controller "RaffleCtrl", ($scope) ->

  $scope.entries = [
    {name: "Larry"}
    {name: "Curly"}
    {name: "Moe"}
  ]

and :

application.html.erb
<html ng-app="Raffler">

Using Rails 4.1.7 ; very new angular user.

Avatar

Hi,

One issue I am having at the moment is that when i search i get the new results fine but the new paginated links do not go anywhere, they just fire a get request

"http://localhost:3000/public/rehome_=1415284148389&animal_town=Pontypool&animal_type=Dog&page=2&rehomed=false&utf8=%E2%9C%93"

but the content does not change

Anyone have any ideas?

Avatar

I have written an article on integration testing using Capybara and RSpec, check out:Stripe Recurring Billing Part 3