RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

For people having problems installing it on Fedora (15/16 Live CD) and my own future reference:

note: the curl version somehow did not work on my Fedora 16 (it worked on Fedora 15 though)

  • which zsh => /bin/zsh

  • chsh -s /bin/zsh

or just chsh and choose the shell.

  • Logout / Restart

  • change .zshrc as mentioned by Ryan.

  • Copy rvm specific stuff from .bashrc to .zshrc

  • rvm --default use gemsetname (e.g 1.9.2@rails3)

Avatar

Thanks Ryan, hope this type of episodes we will see more in future. While doing development we come across issues, day to day troubleshooting, issues faced, new findings, concepts which I have shared with rest of the world through http://rorguide.blogspot.com/. Hope this will be helpful to beginners too.

Avatar

I don't have a ton of experience coding (although I've written working web apps w/ db backend in ASP years ago), so I'm probably missing something obvious, but I cannot for the LIFE of me figure out why I'm getting the following error:

undefined method `search' for #Class:0x4601c10

I'm not following this tutorial exactly but rather using a combination of this and https://we.riseup.net/rails/simple-search-tutorial, which is similar in a lot of respects. The main difference seems to be that the other tutorial uses this in the form declaration:

<%= form_tag :controller => 'sheets', :action => 'search', :method => 'get' do %>

This is because my app is set up a little bit differently. What am I missing here? I can see where the error IS (i.e. Project.search in the controller file) but I don't know how to fix it, and I've googled till I can't see straight.

Thanks.

Avatar

Thanks for the updated routes code for Rails3 Juani, just what I was looking for!

Avatar

Anyone know of a way to have certain cron tasks only happen on certain roles in Capistrano? Such as clearing cache files on every app server, but only running a report generator on one utility server. So far I haven't figured out a nice way to get whenever to support conditional jobs.

Avatar

does this works well along a mulit user rvm installation? will it correctly load my rails environment based on the .rvmrc file?

Avatar

I implemented the code as in the example, but for me it's taking money twice: first with request_payment and then with create_recurring_profile (probably because the subscription starts today and the customer needs to pay today?)

Does anyone else have this problem?

Avatar

Edit: For Facebook you will also need to give them your mobile phone number (they will offer you a link).

Avatar

Just a note that if you're using Heroku for hosting your production app, they recently released the Heroku Scheduler (heroku blog) which they recommend using instead of Cron. It's much easier to use but I believe only lets you get down to every 10 minutes.

Avatar

Just wanted to share with you some problems I had and how solved them when setting Twitter, Facebook and Google.

Avatar

signed up for pro specifically for this episode. Thanks!

another +1 for a follow up on handling cancellations and insufficient funds

Avatar

After the user typed his information, and when you're handling the callback you just check if the authentication the user typed in exists in your database. If it doesn't, then it's a register. Is this what you meant?

Avatar

Take a look at the beginning of episode #304, Ryan explains which things have changed in Omniauth. Maybe it will help.

Avatar

If we want to run this tutorial in higher versions of rails > 3.1.
please add gem 'prototype-rails' in your Gemfile.Since prototype js has been used in this screencasts.

or try to remove this from your environment file
config.action_view.debug_rjs = true

Avatar

What if I want to use HAML in the presenter? I added lines to BasePresenter like so:

ruby
class BasePresenter
  include Haml::Helpers  # access HAML

  def initialize(object, template)
    @object = object
    @template = template
    init_haml_helpers  # init the helpers
  end

  def self.presents(name)
    define_method(name) do
      @object
    end
  end

  def h
    @template
  end

  #---------------------------------------------

  def show_field( label, contents, options=nil )
    haml_tag 'tr' do
      haml_tag 'td.label', label, options
      haml_tag 'td.field', contents ||= ''
    end
  end

end

and used it like this in show.haml:

ruby
- presenter.show_field('Efficacy', presenter.efficacy)

and I can show that show_field is being called, but no HTML is produced. Maybe it is in the wrong context.

Avatar

Hello - Excellent tutorial.

Thank you djensen47 for pointing out the need to add

include Mongoid::MultiParameterAttributes

in model definitions when using Date or DateTime. Without it, the datetimes were being persisted as a set of discrete values (one each for day, month and year parts) and mongoid could not corece them back into a DateTime for display etc.

BTW I'm using mongoid 2.4.0 with Rails 3.1.3 and Ruby 1.9.3p0 and it did not work correctly until I made your addition. Cheers!

Avatar

Shouldn't this line:

ruby
klass ||= "{object.class}Presenter".constantize

be

ruby
klass ||= "#{object.class}Presenter".constantize

?

Avatar

yes, and it's also definitely worth checking out Ruby Monk as well as The Ruby Game

Avatar

Okay: problem was very n00bey :D I accidentally removed the csrf meta token from my layout. That caused the session reset.

Avatar

I would like to not have to update the password every time I modify a user. How can I leave the password field blank and just ignore the validation for the password? Does anybody know how?

Avatar

Ctrl-f, rails 3.1... and I find my savior Jakub!

Avatar

Found the problem:

# ~/.zshrc
bindkey -v

This enables "vi mode", meaning vi command mode is activated when esc (or meta/option) is pressed. Comment this line out to revert to regular bash shortcuts.

Avatar

Anyone else having trouble with shortcuts in zsh on Lion? eg after keying option-b (go back one word), other shortcuts like ctrl-e (go to end) no longer work. Delete and option-delete don't work at all. Terminal is configured with "Use option as meta key" checked. I tried @ErikHick's vt100 suggestion but that didn't help. I'm not using oh-my-zsh, just plain zsh with thoughtbot/laptop settings.

Avatar

Actually, bcrypt-ruby uses this method to parse the digest string:

def split_hash(h)
  _, v, c, mash = h.split('$')
  return v, c.to_i, h[0, 29].to_str, mash[-31, 31].to_str
end

The return of which is [version, cost, salt, hash]. So:

version: '2a'
cost: 10
salt: '$2a$10$QIFk4ytMIzE03/njtSMFme'
hash: 'dzhTyv8DVMMtWjqnFeW9FcQpBEf.u0.'

It is definitely cool!

Avatar

Hey guys, great cast. It totally works as long I don't use my authentication filter in the controller. That before_filter just checks if session[:user_id] exists (derived from "Authentication from scratch"). It redirects to an info page in the latter case that no user is signed in.

So I am using best_in_place from a signed in state but what happens is that the changes are not propagated and additionally the session seems to be reset since I am logged out then.

Anyone an idea?

Avatar

I am trying this example on Rails 3.1 and i get an error

< %= render @message.parent if @message.parent % >

Missing partial messages/message with {:handlers=>[:erb, :builder, :coffee], :formats=>[:html], :locale=>[:en, :en]}. Searched in:

Can someone help?

Avatar

Anyone have experience setting up progressive uploads with an nginx/Unicorn setup? If so, can you post a gist of your setup?

Avatar

If you use Formtastic, there's already a gem with a custom builder for bootstrap:
https://github.com/mjbellantoni/formtastic-bootstrap

Avatar

I am running into the same issue with rails 3.1.3. See below:

error
ruby-1.9.3-p0 :003 > User.new :username => "johnsmith"
ArgumentError: wrong number of arguments (1 for 0)
        from /Volumes/Terra-Nova/jwaldrip/Sites/theirksome/config/initializers/accessible_attributes.rb:6:in `mass_assignment_authorizer'
        from /Volumes/Terra-Nova/jwaldrip/.rvm/gems/ruby-1.9.3-p0/gems/activemodel-3.1.3/lib/active_model/mass_assignment_security.rb:209:in `sanitize_for_mass_assignment'
        from /Volumes/Terra-Nova/jwaldrip/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/base.rb:1744:in `assign_attributes'
        from /Volumes/Terra-Nova/jwaldrip/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/base.rb:1567:in `initialize'
        from (irb):3:in `new'
        from (irb):3
        from /Volumes/Terra-Nova/jwaldrip/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.1.3/lib/rails/commands/console.rb:45:in `start'
        from /Volumes/Terra-Nova/jwaldrip/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in `start'
        from /Volumes/Terra-Nova/jwaldrip/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.1.3/lib/rails/commands.rb:40:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'
app/models/user.rb
class User < ActiveRecord::Base

  # Attributes
  attr_accessible :username, :email, :password, :password_confirmation, :is_admin
  attr_accessor :password

  # Callbacks
  before_save :encrypt_password

  # Relationships
  has_many :irks

  # Validation
  validates_confirmation_of :password
  validates_presence_of :password, on: :create
  validates :password, presence: true, length: { in: 3..20 }

  validates :username, presence: true, uniqueness: true, length: { in: 3..20 }
  validates :email, presence: true, email: true, uniqueness: true

  # User Authentication
  def self.authenticate(email, password)
    user = find_by_email(email)
    if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
      user
    else
      nil
    end
  end

  # Password Encryption
  def encrypt_password
    if password.present?
      self.password_salt = BCrypt::Engine.generate_salt
      self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
    end
  end
end
config/initializers/accessible_attributes.rb
class ActiveRecord::Base
  attr_accessible
  attr_accessor :accessible
  
  private
  def mass_assignment_authorizer
    if accessible == :all
      self.class.protected_attributes
    else
      super + (accessible || [])
    end
  end
end
Avatar

RVM has a helpful page on integration with ZSH:
http://beginrescueend.com/integration/zsh/

It recommends not using the ZSH bundler plugin and adding __rvm_project_rvmrc to the bottom of .zshrc if using iTerm2+"Reuse Previous Tab's Directory"

Avatar

I like the little gems (no pun intended) you pick up watching these - like this is the second or third time I've seen you use the delegate method and I'm starting to use it...

Avatar

What I mean with my last quote is the following: The idea is NOT to know how to build, for example, Active Record, but understanding better the code and the process - through a reading for example?

Avatar

I wish I had had this when I was first learning Rails. I felt pretty lost, and this is exactly the information I could have used.

I didn't know about Balsamiq, I'll definitely be trying that out on my next project.

Thanks for another awesome cast!

Avatar

Great episode Ryan! I really like the introduction of Basalmiq as I've just discover this tool!

I would really LOVE to see some Ruby screencasts here (even if it's "Rails"-cast), starting with basics to metaprogramming. It could be a great, great addition associated with your "classic" rails screencasts.

I have to say my bigger problem is, even after reading few ruby books, that is still hard (for me) to understand how rails does things under the hood, and to understand WELL all parts of the framework. To be more explicit, Rails does things, but I often question myself with why? and how? (And I think most of this is because I am not enough proficient in Ruby - However, I have to say I'm starting to understand rails better through the study of another framework/language, as I started with rails/ruby)

Your episode 299: "Rails Initialization Walkthrough" was a great starting point of what I mean, and it could be awesome to see more screencasts like this in the future ;) Thanks!

Avatar

I am new to Mac but I noticed that you don't need zsh to have the feature that whenever you open a new tab in the terminal, it will land in the same directory from which you make the new tab. Does anyone else also get that?

Avatar

Has anyone tried to do this with a has_many :through association?

Avatar

For anyone who is interested, this is my form builder using the Twitter Bootstrap css framework.

https://gist.github.com/1526162

Avatar

This was amazing... thanks for taking the time to explain things so clearly Ryan.

One small question: When refactoring the collection_check_box code, why do you need to pass record_id and attribute through the send method when calling them on record and object, respectively?

UPDATE:

After looking at the ruby send method documentation, is it because record_id and attribute are passed in as symbols?

Avatar

I think there is a small mistake for the Linux command to set the default Ruby version, instead of global you should use default:

rvm use 1.9.3 --default

Avatar

Have you managed to get it working in production? I'm having trouble getting it working in test & it's working fine in Producton!?

Avatar

No it doesn't but it's pretty easy to do without the 'acts_as_list' gem.

def sort_figures
@report = Report.find(params[:id])
params[:figures].each_with_index do |id, index|
@report.figures.find(id).update_all({position: index+1})
end
render nothing: true
end

Avatar

In case anyone is wondering on how to use the custom builder by default:

ActionView::Base.default_form_builder = LabeledFormBuilder

Avatar

Wow. Just finished it. One of your best Ryan. And this is exactly what I was looking for. I am using the Twitter Bootstrap now and I plan on wrapping all of this into a bootstrap form builder.

SWEET!

Avatar

Great screencast Ryan!

I've also written a zero to hero guide on my blog (it's now also returned with the help! factoid in the official rails chat room) - check it out here: Best Way to Learn Ruby & Rails

Avatar

Confused about what version of Ruby to use. I thought it was suggested to use 1.9.2 for Rails 3 and 3.1, but I see you are using 1.9.3. Does it matter which is used? Is 1.9.3 better?

Avatar

OSX is shipped with precompiled Ruby 1.8.7. Homebrew uses ruby files (called recipes) to execute bash statements. Though, Ruby is preinstalled on MacOS, developers prefer using RVM to manage ruby versions/gemsets.

Avatar

Homebrew shouldn't need ruby. It's just a nice packaging system over macports for mac. For instance you can install mysql , libxml2, etc from homebrew. You can just go to the rvm site, run the instructions in your terminal to setup rvm. Rvm is for managing ruby versions . Install ruby through rvm . Nothing is dependent of homebrew here.