RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

If you are using a Debian distro like Ubuntu, you can do what is suggested in this StackOverflow answer as it worked for me.

Avatar

switched the secret access key and access key.... works

Avatar

Anybody knows what this error is about?

ruby
AuthFailure => AWS was not able to validate the provided access credentials (Fog::Compute::AWS::Error)

happens on
cap rubber:create_staging

is this supposed to be filled in

fog:
credentials:
provider: rackspace
rackspace_api_key: 'XXX'
rackspace_username: 'YYY'
image_type: 123
image_id: 123

Avatar

Hi Scott.

I need to install Libsndfile as a binary instance... How is that done, do you know?

Avatar

I second that. We got the backbone.js but I'd like to see something that isn't quite so heavy.

Avatar

Stuart, thanks for the explanation of why having require_tree . earlier in the manifest causes this issue. It wasn't obvious to me.

Avatar

Does this setup with pow and nginx interfere with mongoid?

I get a routing error with "uninitialized constant Model::Mongoid" when I use a Mongoid model.

The only thing I can think of is that the proxy some how gets in the way.

Has any one else experienced this?

Avatar

If you use S3 as storage

ruby
def profile_picture_geometry(style = :original)
    @geometry ||= {}
    profile_picture_path = (profile_picture.options[:storage] == :s3) ?   profile_picture.url(style) : profile_picture.path(style)
    @geometry[style] ||= Paperclip::Geometry.from_file(profile_picture_path)
end
Avatar

Thanks Ryan for this episode. Yet to watch this. I would like to know whether this work with Ruby1.8.7?

Avatar

Does anyone have any suggestions on how I would start content always from next page when it's breaking between pages.

Example - I have list of photos which i am exporting to pdf.
While listing some photos are cutting in two parts first is coming in one page and remaining coming on other page so how we start photo which is breaking in two part should always display from starting of the page

Avatar

How would you do this if your model is not ActiveRecord based? Since I'm not using ActiveRecord, I don't have access to the accepts_nested_attributes_for method.

Avatar

seperate before/after:

ruby
Before do |scenario|
  if scenario.respond_to? :scenario_outline
    feature = scenario.scenario_outline.feature
  else
    feature = scenario.feature
  end

  cassette_name = "#{feature.name.split("\n").first}/#{scenario.name}"
  VCR.insert_cassette(cassette_name)
end

After do
  VCR.eject_cassette
end
Avatar

A common misconception is that a ||= b is equivalent to a = a || b, but it actually behaves like a || a = b.

Avatar

Yeah, your playbacks always cut off the last couple of seconds, for me. If nothing else pad it with music that fades out. This way, we will at least hear everything you say and know we've reached the end.

Avatar

Nice idea. It should be in rails like built-in validation. Thank's Ryan.

Avatar

So, I've been struggling to figure out what is failing.. Everything installs and setups up fine...code is deployed to the correct location. Both unicorn and nginx are started, but when I hit the site all I get is the Welcome to nginx screen. I even doubled checked that the default site for nginx was gone...any ideas where I could look to figure out what is happening?

Avatar

Great episode. Transition went smoothly

Avatar

note that if you precompile your assets in dev, you'll need to mv or rm /public/assets if you want any future changes to a .css.sass file to show up. At least that is the case here :)

Avatar

Using Foreman with Upstart is the way to go these days. This is a decent post on it.

Avatar

In fact, a ||= "something" is equivalent to a = a || "something"
What does this mean?
a = a || "something" -> left operand in a || "something" isn't defined, evaluates to nil, which is false, then right operand needs to be evaluated as well, so assigning a value.

That's an idiom that came from C language. Try that in irb and you see there's no error at all.

Avatar

Hi Ryan - I wanted to clarify how the object is used in can?. For example...say you have "@users.each do |u|"...with can? inside the loop. I notice many examples freely use either "User" or "u" as the object in can?. Is there a difference? It seems like calling the model as the object "can? :edit, User" would signify "current_user can edit users" whereas "can? :edit, u" would signify "current_user can edit this user"...since u is specific to a record and User is general to the model. Maybe Im putting too much thought in to this. Just wanted to clarify.

THANKS FOR CREATING CANCAN!

Avatar

Thanks, I had the same issue, and that was it - recompiling assets helped!

Avatar

Sure I have used it to send email, and it works great.

It was pretty simple. The send_confirmation looks up the user, and then gets their email address and whatever other information needed.
QC.enqueue("UserMailer.send_confirmation", @user.id)

You may want to enable delivery errors in your development.rb config file. Maybe there's a failure you are not seeing.
config.action_mailer.raise_delivery_errors = true

Avatar

This worked for me on Ubuntu 12.04:

ruby
namespace :imagemagick do
  desc "Install the latest release of ImageMagick and the MagickWand Dev Library"
  task :install, roles: :app do
    run "#{sudo} apt-get -y update"
    run "#{sudo} apt-get -y install imagemagick libmagickwand-dev"
  end
  after "deploy:install", "imagemagick:install"
end
Avatar

Thank you, I was wondering why the group didn't exist.

Avatar

I am running into the same issue and can't seem to figure out how to fix it, have you been able to figure it out yet?

Avatar

I needed to validate permission of a polymorphic association with CanCan.

Hope this helps someone:

ruby
validate :can_comment?

def can_comment?
  if Ability.new(user).cannot?(:comment, commentable_type.classify.constantize.find(commentable_id))
    errors.add(:base, :cannot_comment)
  end
end

Better explanation in this Gist.

Avatar

i get this on the very next page after the user signs up using fb using the oauth fb video tutorial here (and devise oauth).

this error is triggered by
= current_user.facebook.get_object("me").to_yaml

anybody dealt with this?

OAuthException: Error validating access token: Session has expired at unix time 1345496400. The current unix time is 1345499103.

Avatar

I get ArgumentError: wrong number of arguments (1 for 0) for this call:

ruby
first = date.beginning_of_month.beginning_of_week(START_DAY)

If I try it in the console it also gives me the same error. Date.today.beginning_of_month.beginning_of_week(:sunday)

Avatar

This is weird. I have a model called calendar_event. So, in the .coffee file I have

jQuery ->

$('#calendar_event_starts_at').datepicker
dateFormat: 'yy-mm-dd'

which isn't firing datepicker as expected. Does the multi-word model have an effect on jQuery? I've used datepicker before without issue, but all models have been of one word.

Thanks,

Steve

Avatar

Sounds like this could save some requests esp. when finding an address.

Thank you.

Avatar

Sometimes after trying to edit a field, best in place rejects the edit. When I try clicking on the same field to edit again, it doesn't let me click until I refresh. Any idea why this happens?

In these cases, no error message flashes. In the cases where the error message does flash, I can re-enter the invalid data as normal. So the two seem to co-occur.

Any help is greatly appreciated!

Avatar

Thanks for posting, very useful.

Although you shouldn't need the id => false in the create_table declaration. The example given from Matt Bauer from what I can see is using the id as the bigint column whereas here we have replaced it with tweet_id. I have tested this myself and I can confirm it works.

ruby
class CreateLatestTweets < ActiveRecord::Migration
  def change
    create_table :latest_tweets do |t|
      t.integer :tweet_id, :limit => 8
      t.text :content
      t.timestamp :posted_at

      t.timestamps
    end
  end
end
Avatar

i would like to see how u insert a datepicker when u have to change the format to the user, but to save on the database u have to change the format back to rails.

its easy to change the format from / to - only, i really got trapped when i need to format 2012-06-31 to 31/06/2012 to the user and save correctly to the database... thats what i really need to know

Avatar

Thank you for answering yourself as this is just what I was looking for :)

Avatar

You may get this error in Rails 3.2

N+1 Query detected
Country => [:states]
Add to your finder: :include => [:states]
N+1 Query method call stack

Add includes to Country as below

<%= pf.grouped_collection_select :state_id, Country.includes(:states).order(:name), :states, :name, :id, :name, include_blank: true %>
Avatar
ruby
@current_user ||= User.find(session[:user_id]) if session[:user_id]

Same stuff as

ruby
if @current_user.nil? && session[:user_id]
  @current_user = User.find(session[:user_id])
end
Avatar

first one works well under ruby 1.9

Avatar

Is the asset pipeline available if you use the defaults from rails-api? I want to use rails for backend REST but ember.js for the frontend and include it as part of my js assets.

Avatar

I am using ruby 1.9.3p194 and rails 3.2.5
when i do @authors = Author.all It works fine by outputing all name from my database.
But @authors = Author.where("name like ?", "%#{params[:q]}%")
is not working. I am using Mongodb.
What could be the solution?

Avatar

nice piece, also for the neat metaprogramming example!

NB: playback cuts off a little, missing the last couple of seconds.