RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

I managed to do it with an "index.js.erb" file in Rails 4. Just add this coffeescript:

coffeescript
$(window).on 'scroll', ->
  if $('.pagination').length
    @url = $('.pagination .next_page').attr('href')
    if url && $(window).scrollTop() > $(document).height() - $(window).height() - 50
      $('.pagination').remove()
      $('#articles').append('<div>')
      $('#articles div').last().load @url+' #articles', ->
          if $('.next_page.disabled').length
            $('.pagination').remove()

This loads the next page then uses the pagination on the newly loaded page until there are no more next pages. Just replace "#articles" with the id of your container in the script. Skip the index.js.erb and respond_to jazz.

Avatar

These posts are over a year old, did anyone find out as to why to use the techniques in this episodes rather than the nest_form gem?

Avatar

The #196 revised does all this plus the previous video (196) for rails 3, and they mostly work for Rails 4 except for strong parameters adjustments.

Avatar

I've noticed that during deployments, monit will sometimes see that unicorn, sidekiq etc have no pidfiles so will try to start them before the deploy is completed. Does anyone have a solution for this?

I'm thinking of writing a capistrano lockfile when the deploy is started and removing it when finished, then monit can skip its checks for the affected services if the lockfile exists.

Avatar

hi everyone , this is the first time I ever post something here but before I proceed I need to thank every one kind soul that takes the time to help others, like me, not so experienced; thank you thank you thank you...now I have an app that before I add the code above(calendar) was working fine but now it only works on localhost...I am assuming this something related to heroku so
I looked at heroku logs looking for some hints but I can't figure it out...here is the logs9 at least the first 5 lines)

Starting process with command bin/rails server -p 4543 -e $RAILS_ENV
2013-12-02T22:10:36.246379+00:00 app[web.1]: /app/config/application.rb:5:in <top (required)>': undefined local variable or methodconfig' for main:Object (NameError)

Avatar

Rails 4

ruby
def sort
  params[:stack].each_with_index do |id, index|
     Stack.where(id: id).update_all({position: index+1})
  end
  render nothing: true
 end
Avatar

Nice tutorial!

What if I'd like to migrate my users to another authentication system such as 'Devise' later on?

Avatar

How does Geocoder comply with section 10.1.3b in the Google Maps TOS?

No Pre-Fetching, Caching, or Storage of Content. You must not pre-fetch, cache, or store any Content, except that you may store: (i) limited amounts of Content for the purpose of improving the performance of your Maps API Implementation if you do so temporarily (and in no event for more than 30 calendar days), securely, and in a manner that does not permit use of the Content outside of the Service; and (ii) any content identifier or key that the Maps APIs Documentation specifically permits you to store. For example, you must not use the Content to create an independent database of "places" or other local listings information.

Avatar

+1 for an extension to this cast... perhaps writing a Spree Extension :)

Avatar

NOTE:
This tutorial example creates a c1.medium instance type on the EC2 and that'll cost $0.145 per Hour.

The image_type for the instance is defined at around line 89 of rubber.yml. To avoid accidental large bill from Amazon, you should choose another instance type from: EC2 pricing

Avatar

I must be missing something obvious, but although I have got the basic version working fine, I can't figure out how to implement the UI version with all of the built in widgets. Does anyone know how?

Thanks in advance!

Avatar

I am trying to click a list of links with Mechanize gem, but apparently Mechanize's links_with(criteria) is not properly filtering based on the criteria. For debugging purposes, I am only printing out the link.

The following script is printing out most (all?) links on the page:

ruby
require 'mechanize'

agent = Mechanize.new
url = "http://www.fearlessphotographers.com/location/470/sul-do-brasil"

agent.get(url)
agent.page.links_with(:text => /[VIEW FULL PROFILE]/).each do |link|
    puts link.text
end

And if I change the (:text => /[VIEW FULL PROFILE]/) to (:text => "VIEW FULL PROFILE") then no link at all gets printed.

I can't understand what I am doing wrong. Any thoughts?

Avatar

hey Jose,

can you point me to some resources as to how I can accomplish this with js/jquery. :D

Avatar

I needed to use radio_button_tag and got it to work using Ryan's example with:
File: people_datatable.rb

ruby
delegate :params, :h, :link_to, :radio_button_tag, :number_to_currency, to: :@view

private

  def data
    people.map do |person|
      [
          radio_button_tag('person', person.id),
          h(person.last_name),
          h(person.first_name)
      ]
    end
  end
Avatar

I have a friend who wrote a Ruby wrapper to the Plotly REST API. You can use it to make interactive, web-based graphs. You can both download your graphs and embed them as an iframe.

It's on GitHub here: https://github.com/plotly.

You can see examples at https://plot.ly.

Avatar

Can I enable/disable the alerts only for some paths/controllers ?
I got a lot of alerts on activeadmin and I cannot change that code.
For example I get one for the column :user_type line:

app/admin/admin_users.rb
  index do
    column :email
    column :first_name
    column :last_name
    column :user_type

    default_actions
  end
Avatar

Hi

How to start resque in verbose mode for production.

Thanks in advance!!

Avatar

Well I'm a comparative newbie here but with my limited Rails4 work I would suggest whitelisting by doing this (using Ryan's example). In the private area of the books controller add the author_ids to the book_params:

rails
def book_params
 params.require(:book).permit( ... many attributes...., :author_ids => [])
end
Avatar

Thanks so much! This definitely helped me out as well.

Avatar

I am using rails 3.2.1 omniauth-twitter (0.0.9)

But in twitter callback phase I receive this error

TypeError "can't convert nil into String"

omniauth-twitter (0.0.9) lib/omniauth/strategies/twitter.rb:22:in +'
omniauth-twitter (0.0.9) lib/omniauth/strategies/twitter.rb:22:in
block in class:Twitter'
omniauth (1.1.4) lib/omniauth/strategy.rb:106:in instance_eval'
omniauth (1.1.4) lib/omniauth/strategy.rb:106:in
block in compile_stack'
omniauth (1.1.4) lib/omniauth/strategy.rb:105:in each'
omniauth (1.1.4) lib/omniauth/strategy.rb:105:in
inject'
omniauth (1.1.4) lib/omniauth/strategy.rb:105:in compile_stack'
(eval):7:in
info_stack'
omniauth (1.1.4) lib/omniauth/strategy.rb:322:in info'
omniauth (1.1.4) lib/omniauth/strategy.rb:335:in
auth_hash'
omniauth (1.1.4) lib/omniauth/strategy.rb:362:in callback_phase'
omniauth-oauth (1.0.1) lib/omniauth/strategies/oauth.rb:58:in
callback_phase'
omniauth (1.1.4) lib/omniauth/strategy.rb:226:in callback_call'
omniauth (1.1.4) lib/omniauth/strategy.rb:182:in
call!'
omniauth (1.1.4) lib/omniauth/strategy.rb:164:in call'
omniauth (1.1.4) lib/omniauth/strategy.rb:184:in
call!'
omniauth (1.1.4) lib/omniauth/strategy.rb:164:in call'
omniauth (1.1.4) lib/omniauth/builder.rb:49:in
call'

I have already updated omniauth-twitter gem and it remains at the same version mentioned above.

What could be the possible reason?

Avatar

hey @ryan! I used your home brew in a rails 4 app, but the self.by_votes method doesn't seem to work. As is... it orders things, but it duplicates each item for each time they've been voted on. I was wondering if you had any ideas?

Avatar

I'm running into an issue with Rails (v4) images (ex: the arrows) If I put it into assets/images/
do I have to do precompiling? The asset pipeline is really confusing me

Avatar

You'll need to set the time zone to the current user's time zone. It's probably setting it to Heroku's UTC time.

Before creating the calendar, set the time zone in the controller.

Avatar

Thank you very much Florian!

Avatar

I don't know if you still need... I used with strong parameters with this:

ruby
def self.import(file)
    CSV.foreach(file.path, headers: true) do |row|
      product = find_by_id(row["id"]) || new
      parameters = ActionController::Parameters.new(row.to_hash)
      product.update(parameters.permit(:name,:price))
      product.save!
    end
  end

and it worked. I don't know if it is optimal

Avatar

Ryan says that actually friendly_id history doesn't work for old records but only for new ones.
That is not true anymore.
You just need to create friendly_id table before adding slug column and save your object.
Like this:

ruby
create_table :friendly_id_slugs do |t|
  t.string   :slug,           :null => false
  t.integer  :sluggable_id,   :null => false
  t.string   :sluggable_type, :limit => 40
  t.datetime :created_at
end
add_index :friendly_id_slugs, :sluggable_id
add_index :friendly_id_slugs, [:slug, :sluggable_type], :unique => true
add_index :friendly_id_slugs, :sluggable_type

add_column :articles, :slug, :string
add_index :articles, :slug
Article.reset_column_information
Article.find_each(&:save)

and everything will work fine as expected.

I also suggest, if you don't want to loose old urls, to execute this code after adding frindly_id_slugs and before adding column to articles

ruby
Article.find_each do |article|
    execute "INSERT INTO friendly_id_slugs(
            slug, sluggable_id, sluggable_type, created_at)
            VALUES ('"+"#{article.id}-#{article.name}".parameterize+"', #{article.id}, 'Article', current_timestamp);"

    end

current_timestamp if you are using postgresql

Avatar

I am having problem with the search function.when search function is assigned the page will reload and can't find out the search element.How to solve this error?.

Avatar

I did everything as given in the tutorial, but somehow i couldn't seem to generate latitude and longitude automatically when i leave the values blank.

Throws an error infact in "show.html.erb" in this particular line
//
<% for location in @location.nearbys(10) %> -> error line

undefined method `each' for nil:NilClass -> error
//

Can any1 pls help. Thanks in advance.

PS: Im using rails 4 so i don't have to write the attr_accessible line. Just to mention.

Avatar

Dana:

Make sure you have included the code for basic authorization in application_controller.rb

ruby
def authorize
  redirect_to login_url, alert: "Not authorized" if current_user.nil?
end

and then make sure to invoke it in all your controllers:

ruby
class ArticlesController < ApplicationController
  
  before_filter :authorize, only: [:edit, :update]  # could also use before_action in Rails 4
Avatar

How can you actually load in the comments inside of the div?

Avatar

Yes I have,

The gem was renamed to "foundation-rails" but still works like the old one.

ruby
gem "foundation-rails", "~> 5.0.2.0"
sh
rails g foundation:install

...

Avatar

followed the tutorial, but for the life of me cannot get this to work on rails 3.2.13 and ruby 2.0.0.
basically i get the following error with the controller's 'show' action:

ActiveRecord::RecordNotFound - Couldn't find Artwork with id=pirmais:
(gem) activerecord-3.2.13/lib/active_record/relation/finder_methods.rb:343:in find_one'
(gem) activerecord-3.2.13/lib/active_record/relation/finder_methods.rb:314:in
find_with_ids'
(gem) activerecord-3.2.13/lib/active_record/relation/finder_methods.rb:107:in find'
(gem) activerecord-3.2.13/lib/active_record/querying.rb:5:in
find'
(gem) cancan-1.6.9/lib/cancan/model_adapters/abstract_adapter.rb:20:in find'
(gem) cancan-1.6.9/lib/cancan/controller_resource.rb:114:in
find_resource'
(gem) cancan-1.6.9/lib/cancan/controller_resource.rb:68:in load_resource_instance'
(gem) cancan-1.6.9/lib/cancan/controller_resource.rb:32:in
load_resource'
(gem) cancan-1.6.9/lib/cancan/controller_resource.rb:25:in load_and_authorize_resource'
(gem) cancan-1.6.9/lib/cancan/controller_resource.rb:10:in
block in add_before_filter'
(gem) activesupport-3.2.13/lib/active_support/callbacks.rb:451:in _run__369076481886043173__process_action__2635143404322980588__callbacks'
(gem) activesupport-3.2.13/lib/active_support/callbacks.rb:405:in
run_callback'
(gem) activesupport-3.2.13/lib/active_support/callbacks.rb:385:in _run_process_action_callbacks'
(gem) activesupport-3.2.13/lib/active_support/callbacks.rb:81:in
run_callbacks'
(gem) actionpack-3.2.13/lib/abstract_controller/callbacks.rb:17:in process_action'
(gem) actionpack-3.2.13/lib/action_controller/metal/rescue.rb:29:in
process_action'
(gem) actionpack-3.2.13/lib/action_controller/metal/instrumentation.rb:30:in block in process_action'
(gem) activesupport-3.2.13/lib/active_support/notifications.rb:123:in
block in instrument'
(gem) activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in instrument'
(gem) activesupport-3.2.13/lib/active_support/notifications.rb:123:in
instrument'
(gem) actionpack-3.2.13/lib/action_controller/metal/instrumentation.rb:29:in process_action'
(gem) actionpack-3.2.13/lib/action_controller/metal/params_wrapper.rb:207:in
process_action'
(gem) activerecord-3.2.13/lib/active_record/railties/controller_runtime.rb:18:in process_action'
(gem) actionpack-3.2.13/lib/abstract_controller/base.rb:121:in
process'
(gem) actionpack-3.2.13/lib/abstract_controller/rendering.rb:45:in process'
(gem) actionpack-3.2.13/lib/action_controller/metal.rb:203:in
dispatch'
(gem) actionpack-3.2.13/lib/action_controller/metal/rack_delegation.rb:14:in dispatch'
(gem) actionpack-3.2.13/lib/action_controller/metal.rb:246:in
block in action'
(gem) actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:73:in call'
(gem) actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:73:in
dispatch'
(gem) actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:36:in call'
(gem) journey-1.0.4/lib/journey/router.rb:68:in
block in call'
(gem) journey-1.0.4/lib/journey/router.rb:56:in each'
(gem) journey-1.0.4/lib/journey/router.rb:56:in
call'
(gem) actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:612:in call'
(gem) warden-1.2.1/lib/warden/manager.rb:35:in
block in call'
(gem) warden-1.2.1/lib/warden/manager.rb:34:in catch'
(gem) warden-1.2.1/lib/warden/manager.rb:34:in
call'
(gem) actionpack-3.2.13/lib/action_dispatch/middleware/best_standards_support.rb:17:in call'
(gem) rack-1.4.5/lib/rack/etag.rb:23:in
call'
(gem) rack-1.4.5/lib/rack/conditionalget.rb:25:in call'
(gem) actionpack-3.2.13/lib/action_dispatch/middleware/head.rb:14:in
call'
(gem) actionpack-3.2.13/lib/action_dispatch/middleware/params_parser.rb:21:in call'
(gem) actionpack-3.2.13/lib/action_dispatch/middleware/flash.rb:242:in
call'
(gem) rack-1.4.5/lib/rack/session/abstract/id.rb:210:in context'
(gem) rack-1.4.5/lib/rack/session/abstract/id.rb:205:in
call'
(gem) actionpack-3.2.13/lib/action_dispatch/middleware/cookies.rb:341:in call'
(gem) activerecord-3.2.13/lib/active_record/query_cache.rb:64:in
call'
(gem) activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in call'
(gem) actionpack-3.2.13/lib/action_dispatch/middleware/callbacks.rb:28:in
block in call'
(gem) activesupport-3.2.13/lib/active_support/callbacks.rb:405:in _run__4384397577656740839__call__2610537326534929259__callbacks'
(gem) activesupport-3.2.13/lib/active_support/callbacks.rb:405:in
run_callback'
(gem) activesupport-3.2.13/lib/active_support/callbacks.rb:385:in _run_call_callbacks'
(gem) activesupport-3.2.13/lib/active_support/callbacks.rb:81:in
run_callbacks'
(gem) actionpack-3.2.13/lib/action_dispatch/middleware/callbacks.rb:27:in call'
(gem) actionpack-3.2.13/lib/action_dispatch/middleware/reloader.rb:65:in
call'
(gem) actionpack-3.2.13/lib/action_dispatch/middleware/remote_ip.rb:31:in call'
(gem) better_errors-0.8.0/lib/better_errors/middleware.rb:84:in
protected_app_call'
(gem) better_errors-0.8.0/lib/better_errors/middleware.rb:79:in better_errors_call'
(gem) better_errors-0.8.0/lib/better_errors/middleware.rb:56:in
call'
(gem) actionpack-3.2.13/lib/action_dispatch/middleware/debug_exceptions.rb:16:in call'
(gem) actionpack-3.2.13/lib/action_dispatch/middleware/show_exceptions.rb:56:in
call'
(gem) railties-3.2.13/lib/rails/rack/logger.rb:32:in call_app'
(gem) railties-3.2.13/lib/rails/rack/logger.rb:16:in
block in call'
(gem) activesupport-3.2.13/lib/active_support/tagged_logging.rb:22:in tagged'
(gem) railties-3.2.13/lib/rails/rack/logger.rb:16:in
call'
(gem) quiet_assets-1.0.2/lib/quiet_assets.rb:18:in call_with_quiet_assets'
(gem) actionpack-3.2.13/lib/action_dispatch/middleware/request_id.rb:22:in
call'
(gem) rack-1.4.5/lib/rack/methodoverride.rb:21:in call'
(gem) rack-1.4.5/lib/rack/runtime.rb:17:in
call'
(gem) activesupport-3.2.13/lib/active_support/cache/strategy/local_cache.rb:72:in call'
(gem) rack-1.4.5/lib/rack/lock.rb:15:in
call'
(gem) actionpack-3.2.13/lib/action_dispatch/middleware/static.rb:63:in call'
(gem) railties-3.2.13/lib/rails/engine.rb:479:in
call'
(gem) railties-3.2.13/lib/rails/application.rb:223:in call'
(gem) rack-1.4.5/lib/rack/content_length.rb:14:in
call'
(gem) railties-3.2.13/lib/rails/rack/log_tailer.rb:17:in call'
(gem) rack-1.4.5/lib/rack/handler/webrick.rb:59:in
service'
/Users/janis/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/webrick/httpserver.rb:138:in service'
/Users/janis/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/webrick/httpserver.rb:94:in
run'
/Users/janis/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

and from what i understand there is problem with this line in:
(gem) activerecord-3.2.13/lib/active_record/relation/finder_methods.rb

343 raise RecordNotFound, "Couldn't find #{@klass.name} with #{primary_key}=#{id}#{conditions}"

Any help appreciated. by the way, had the same error when tried to implement friendly_id gem.

Avatar

How does this work? I am having the same issue getting a Missing Template.

Avatar

Great railscast! I didn't like the gmail option but found this awesome blog post which explains how to start postfix on your mac and send mail from that - no passwords or environment variables required

http://andr.esmejia.com/posts/20-send-real-mail-with-rails-from-your-development-environment-on-mac-os-x

In brief:

in terminal do
sudo postfix start

In config/environments/development.rb :
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "localhost",
:port => 25,
:domain => "whatever.com",
}

Worked great on Mac OSX Lion, Rails 3.2

Remember to look in your gmail spam folder for the email (on gmail.com not just Mac Mail app)

Avatar

I'm struggling with how to pass parameters through to the .rb script. Been battling away for weeks now, with no success. :-(

Avatar

Has anyone been able to install Foundation 5?

Avatar

Buen vídeo felicitaciones me gusto

Avatar

Hi there!

I am trying to authenticate using warden for JSON based APIs(web services), I have defined my password strategy inside config/initializers as password_strategy.rb which has authenticate! method defined inside it. When I call this authenticate! method from sessions controller's create method, params inside authenticate! method are getting empty some how although I can see my params inside create method of sessions controller, not sure what magic is happening behind the scene.

Since my params are empty my authentication is detected as unauthorized and I am redirected to sessions new method for logging in again even my login credentials are correct.

Any help would be appreciated.

Thanks in advance.

Avatar

I can log in and log out, but I can also use all the links, add content or delete. So my authentication is not quite working right. My app says I'm successfully logged in, or logged out, but it doesn't matter. Either way there is full access to the rest of the app.

I followed all the direction in this tutorial, and thought it was working until I realized I could still do anything as an unlogged user.

Any idea where I should look to correct this?

Thank you!

Avatar

can I use the calendar to have a live update for booking stuff? If so then do I have to use ActionController::Live for it?

Avatar

Does anyone know how to add totals with this approach?

Avatar

Did anyone ever figure out how to do this?

Avatar

I am encountering the same problem. It's a shame that this functionality is broken (or removed) from rails 4.

Avatar

I think you can do all this server side validation in the carrierwave uploader (as Ryan used carrierwave for this video).