RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Anyone seeing the pjax links make the "page request" twice? Tried various configurations, but every pjax link makes 2 requests; the first with _pjax=true and the second without _pjax. I've changed the jquery-pjax plugin timeout to 30000 (in ms) and still no luck. I've tried various browsers, and they all are doing the something. Any suggestions?

Avatar

One of the best casts I've seen from you! Big +1 for covering insufficient funds and cancellation. Would be a shame not to do it now that you refactored everything nicely into its own model. :)

Avatar

I managed to figure it out :)

xml
<fieldType name="text" class="solr.TextField" omitNorms="false">
    <analyzer type="index">
        <tokenizer class="solr.StandardTokenizerFactory"/>
        <filter class="solr.StandardFilterFactory"/>
        <filter class="solr.LowerCaseFilterFactory"/>
        <filter class="solr.NGramFilterFactory" minGramSize="1" maxGramSize="15" />
    </analyzer>
    <analyzer type="query">
        <tokenizer class="solr.StandardTokenizerFactory"/>
        <filter class="solr.StandardFilterFactory"/>
        <filter class="solr.LowerCaseFilterFactory"/>
    </analyzer>
</fieldType>
Avatar

It degrades gracefully for those browsers: jquery-pjax won't intercept clicks when the browser doesn't support pushstate, making the link just a normal link.

Avatar

You can use it this way:

ruby
%div{:data => {"pjax-container" => true}}

It is especially handy when you have several "data-" attributes, this way you don't have to repeat the "data-" part.

Avatar

There is a list of available vagrant box packages which is not including Fedora yet: http://www.vagrantbox.es/

I think you have to create your own through VirtualBox (select your operating system and version at the Create Virtual Machine Wizard, Fedora available?) in consideration of vagrant's guidelines for virtual boxes (http://vagrantup.com/docs/base_boxes.html): https://www.virtualbox.org/manual/ch01.html#gui-createvm

Then you have to package the box through vagrant: http://vagrantup.com/docs/base_boxes.html

Avatar

Anybody know how to configure sunspot/solr to imitate textmate's command-t like searches?

Avatar

it would help if 'safe' were a word i could spell

Avatar

I've just run into 'undefined method `urlsaf_base64' for SecureRandom:Module' on ruby 1.9.2p290

Avatar

jquery-pjax isn't compatible with browsers not supporting pushstate, like all released IEs. A trade-off between convenience and compatibility.

Avatar

I found the answer back in episode #82:

app/controllers/application.rb
class ApplicationController < ActionController::Base
  before_filter :authenticate_if_staging

private

  def authenticate_if_staging
    if Rails.env == 'staging' && request.remote_addr !~ /^192.168.0.\d{1,3}$/
      authenticate_or_request_with_http_basic 'Staging' do |name, password|
        name == 'username' && password == 'secret'
      end
    end
  end

end
Avatar

When u say 'maybe i can do something with rack'
what u mean? what means a rack app?
its something inside rails? its is a modeling pattern? where i can found material about?

Avatar

Wow, thank you VERY MUCH, tguevin!! I will absolutely give it a try! :)

I really appreciate your help.. you've made my day :P

Avatar

It's good, but I have some problems with graphics.

Avatar

I am sure I am just not getting it, but it seems that decorators are just sweeping dirt under the rug. Every time I see changes like this that alter the way apps are written by making them more complex in the name of cleanliness, I feel that it is just one more thing that stands between me and delivering a product to my clients that can be handed off to other developers easily.

I think Jeff is a great guy.. We hung out in Salt Lake City way back and I realized the guy knows his stuff.. This is why I don't want to discount the idea in my head, but are we trying to solve a problem that might not need to be solved? And in turn, are we making it more complex?

Avatar

In the AsciiCasts version there's a typo

data: <%= orders_chart_series(orders, 3.weeks.ago) %>

should say

data: <%= orders_chart_series(order, 3.weeks.ago) %>

Avatar

see my post below Hannes H. - you should be OK.

Avatar

wow - monster cast! love your work Ryan and i'm happy to start paying you some money for Pro as you have taught me so much over the years / every day still! i'm UK based and as Jon Hope said, online payment solutions in the UK are in the dark ages and PayPal is a safe route. i don't think a WPP account is necessary though, a Standard Business account should suffice when using the PP Express Checkout approach (source: https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_ECGettingStarted) ... and i quote from there:

To use Express Checkout, you must have: A Business or Premier account. A Business or Premier account enables you to become a merchant for whom PayPal collects money from buyers for goods or services. PayPal manages these transactions and charges you a small fee and a percentage of the amount collected from the buyer for each transaction.

I hope that helps clear up some confusion for British (and probably German) customers.

Avatar

"rails mailit" don't work, is that "rails new mailit"

Avatar

Hello Ryan

Great screencast.
I was wondering, is it possible to use the pjax functionality with a search button instead of a link?

Whem I try I get this

No route matches [GET] "/undefined"

Avatar

Thank you this was very helpful even though devise has changed a little.

Avatar

How would I make HTTP basic authentication conditional? For example, I have a staging server at work, and I only want to make people authenticate if they access the server from outside the local network.

app/controllers/application.rb
class ApplicationController << ActionController::Base
  http_basic_authenticate_with :realm => "Staging", :name => "user", :password => "password" if :needs_authentication?

private

  def needs_authentication?
    Rails.env == "staging" && request.remote_addr !~ /^192.168.0.\d{1,3}$/
  end

end

With this, I am always prompted to authenticate—even if needs_authentication? explicitly returns false. Moving http_basic_authenticate_with inside a method doesn't seem to work, either—I get an undefined method error.

Avatar

Is Ubuntu the only supported VM for vagrant? It would be nice to have support for Fedora/RH as well.

Avatar

+1 - Handlebars + handlebars_assets gem does this but the templates need to be required into application.js. Would be good if they were automatically compiled from your view directory

Avatar

I also wonder how you would reuse the template with Rails 3.1 assets pipeline and JST?

Avatar

Another thing is that I would probably use Spine.js or similar for infinite scrolling: http://vimeo.com/30796067

Avatar

I love using factories, but for some reason I got frustrated with the factory_girl syntax on a recent project and ended up justing creating my own factories.rb file in the support/ directory that didn't use an external gem.

It is surprisingly easy to do in plain old active record with a familiar syntax, so now I'm not sure it even warrants a separate gem. May just be personal preference though.

ruby
def create_user options={}
  User.create!(user_attributes.merge(options))
end

def user_attributes
  {:email => "user#{user_count}@example.com"}
end

def user_count
  @user_count = @user_count.nil? ? 0 : @user_count += 1
end
Avatar

VCR for me has been a total game changer. Here is a snip-it to create a @vcr tag for cucumber scenarios:

ruby
Around('@vcr') do |scenario, block|
  s_name = scenario.name.underscore.gsub(/[^\w\/]+/, "_")
  f_name = scenario.feature.name.underscore.gsub(/[^\w\/]+/, "_")
  VCR.use_cassette("/scenarios/#{f_name}/#{s_name}", record: :once) { block.call }
end
Avatar

Thanks, that worked great

Avatar

Thanks. This plugin still isn't working for me so I guess that wasn't the issue.

Avatar
ruby
%div(data-pjax-container="true")

Works the same and still looks semantic.

Avatar

How do you create the following div in HAML?

< div data-pjax-container >

We're trying to use this plugin but it's not working and I have a feeling it's because we aren't converting that tag to HAML correctly. Anyone using HAML with this?

Avatar

Take a look at faker for generating unique object data.

Avatar

For others getting Errno::ECONNREFUSED: Connection refused - connect(2) errors when testing Solr-enabled models with Cucumber, put this in your env.rb file:

Sunspot::Rails::Server.new.start
at_exit do
Sunspot::Rails::Server.new.stop
end

This will start up your solr server when your tests start, and shut it down afterwards.

Avatar

I just released a new version of rack-pjax, it includes specs and fixes this issues as well.

Avatar

Nice... however when I run bundle it complains about a gemfile conflict

Bundle
Bundler could not find compatible versions for gem "nokogiri":
  In Gemfile:
    rack-pjax (>= 0) x86-mingw32 depends on
      nokogiri (~> 1.4.4) x86-mingw32

    cucumber-rails (>= 1.1.1) x86-mingw32 depends on
      nokogiri (1.5.0)
Avatar

Here's something I grabbed from the Jasmine Google Group:

By default Jasmine will load files you place in spec/javascripts/helpers. You'll want to put the version of jquery that Rails provides (and jquery-ui, if you're using it) in there in order for Jasmine to be able to load it for your tests. Rails normally provides these to the asset pipeline via the jquery-rails gem, which jasmine doesn't yet interact with.

Hope this helps!

Thanks for using Jasmine!

Rajan

Avatar

There is an error in the notes for pjax_rails. The manifest file should just be

ruby
//= require pjax 
Avatar

I confirm this bug. Steps to reproduce:

  • Visit home page
  • Click on any episode
  • Click on the comments tab
  • Go back
  • Go forward

Edit: It has already been reported on Github

Wael

Avatar

Great screencast! Definitely a +1 for Part II; covering insufficient funds and cancellation would be really appreciated. Thanks!

Avatar

Thanks Ryan.

For those who wants to try out Handlebars, you might want to take a look at
https://github.com/leshill/handlebars_assets

It compiles your handlebars templates with the rest of your assets. Pretty neat

Avatar

Hi Ryan...Are you using this on railscasts.com? I noticed there is a bug...when you are on an episode page, click on one of the tabs like comments (I'm assuming this is using pjax). Then type in google.com and press the back button. It shows the pjax request code instead of reloading the full page.

Avatar

I didn't find this to be the case on my setup, but you may be right as I'm not the most proficient at Ubuntu. Thanks for mentioning.

Avatar

More episodes on deployment are planned, thanks for the feedback.

Avatar

Haha, had a bad cold that really messed with my voice.

Avatar

Good question, needing to run bundle exec depends on how your system is setup. Here I'm only installing gems through bundler (there is no Rails gem installed on the system outside of Bundler) so I need to use bundle exec. It's also a good idea to always use this to ensure you're working with the proper version of the gem.

On my development machine I am using Oh My ZSH with the Bundler plugin which sets up aliases for these commands so I don't need to type in "bundle exec" in development.

Avatar

I'm not certain. Is the mail delivery method working? Try experimenting with it in the Rails console on your production server and see if it sends email.