RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Receiving exceptions by email is great because you can set it up to be notified when this happens. Also if you're providing an in-app interface for browsing exceptions you need to worry about locking it down. Having a database of exceptions is handy, but Gmail search works pretty well.

Avatar

Even just a few tests are better than no tests at all, so start off with the easiest ones to write that will get you the most "bang for your buck".

I find these are generally the high-level request specs like I show in episode 275. Writing request specs is nearly as easy as walking through the app manually, and once it's done you won't have to manually test it next time.

Avatar

This is a controversial subject, but I don't subscribe to the "one assertion per test" way of thinking. It can really slow down tests if you're not sharing data between assertions, especially in high-level request specs. RSpec 2 shows you exactly what line failed the assertion so it is easy to spot in a multi-assert clause.

Avatar

Right, without PJAX I would normally add remote: true option to a link and respond with some JavaScript that renders out only exactly what I need, but that is quite a bit more work. So it is a tradeoff of performance for convenience.

Avatar

Totally blown away by this. It makes AJAX so much easier, the way it should be, thank you

Avatar

Since this screencast I use the request specs for my development.
It works great for understanding of the function and as a direct mapping with use cases from RE.
The only issues I have is speed.
As for most of the specs I need a logged in user. This is therefore created in nearly every before block. Also, the user factory creates certain associated models as well (in this case a company.)

Is there a better/faster way to do it?
Would you use fixtures for those cases?

ruby
describe "Request Management Requests" do
  before do
    @user = create(:user)
    login @user
  end
  
  it "lists open requests" do
    @req = create(:request, :company => @user.company)
    visit requests_path
    page.should have_content(@req.name)
  end
end
Avatar

So after perusing the github pages for pjax, it seems like you could do this:

javascript
$('#pages').bind('start.pjax', function() {
  $('#pages').fadeOut(1000) 
}).bind('end.pjax', function() { 
  $('#pages').fadeIn(1000) 
});
Avatar

pjax is cool and all, but what about adding transition effects?

Avatar

Really interesting. I sometimes used Poirot for this purpose(https://github.com/olivernn/poirot).

Just a notice: isn't "try" a perf killer? You use it in your product helper. (Well just did some benchmark, seems not to be that a problem)

Avatar

Looks good, but: this always renders the full layout? That does seem very wasteful to me. But I guess its a tradeoff between code complexity and performance.

Avatar

Just as I suspected. When I downloaded the jQuery file to my public/javascripts/ folder, Jasmine worked just fine. You can use this as a temporary hack.

Avatar

I don't think there's support for Jasmine and Rails 3.1 yet if you are using the asset pipeline. I'm trying to figure out why my tests aren't working, and it seems to me it is because the jQuery files are loaded when the pages of my Rails app load.

Avatar

What is the way to use Sunspot/Solr with mutiple fields ?
It works fine with a simple form, as explained in the screencast.

For example, I'm looking for how to make a search engine :

<%= form_tag products_search_path, :method => :get do %>

<%= label_tag "Location ?" %>
<%= select_tag :address, "new york".html_safe %>


<%= label_tag "Type ?" %>
<%= text_field_tag :model, params[:type] %>
<%#= select_tag :model, "testtest".html_safe %>


<%= label_tag "Category ?" %>
<%= text_field_tag :category, params[:category] %>

<%= submit_tag "Search" %>
<% end %>

Thanks guys!

Avatar

i was waiting for such a post and its information!
Owner of london escorts

Avatar

is it also possible to run multiple ruby versions at the same time? (e.g. 1.8.7 and 1.9.2)

Avatar

You've inspired me to give Unicorn a try. Quick question: Don't you need to run the following after you've created the /etc/init.d/unicorn symlink to unicorn_init.sh so that Unicorn automatically starts when the server is rebooted?

bash
sudo /usr/sbin/update-rc.d -f unicorn defaults

I found this to be the case for a production server I'm running on Ubuntu. Can't wait to see more episodes on deployment. Thanks!

Avatar

Keep testing!

I know that sounds like a cop out but I had to force myself to just continue to test. Don't feel like you have to hit that holy grail of 100% coverage or even 50%, just keep testing as much as you know how to. The more you do it, the more you'll eventually get to the point that you realize you can code as fast, if not faster through tests and when you realize that, its an exciting thing!

Avatar

This doesn't seem to work with FactoryGirl. When I turn transactional fixtures off, all of my specs depending on FactoryGirl fail. :'(

Avatar

Thanks for this episode, Ryan!

I have only one question: using two (or more) should in one it is o'key or it's just an example?

Avatar

Hi Gregor.

This is really interesting. Any chance that you can share a config example for setting up nginx_http_push_module with faye?

Avatar

Luke, build an app then come back after a year and try to make some changes. You'll wish you had written some tests.

Avatar

I still feel like testing is such a drag, How do I believe?

Avatar

Short answer (via jquery.com) is: PUT and DELETE are not supported by all browsers.

It's more convenient to go with POST.

Avatar

What other considerations should be taken into account to use the PUT method, instead of POST? Is it as simple as changing the method name?

download_types.js.coffee
jQuery ->
  $('#download_types').sortable
    axis: 'y'
    handle: '.handle'
    update: ->
      $.put($(this).data('update-url'), $(this).sortable('serialize'))
routes.rb
resources :download_types do
  collection do
    put 'sort'
  end
end
Avatar

How can I change the password?
I need to validate de "old_password" before change it.

Avatar

Late to the party -- I like Ryan's approach but was troubled by the complexity of the create() method. My modified approach has a Wizard model that saves the current_step as a db field (so it's preserved across sessions) and saves an instance before starting. From there, everything is done in the upate() method, so the controller simplifies down to:

ruby
  def show
    render @wizard.current_step
  end

  def update
    (params[:step] == 'back') ? @wizard.step_back : @wizard.step_forward
    if @wizard.update_attributes(params[:wizard])
      redirect_to wizard_path, :notice => 'update succeeded'
    else
      # the reload restores the previous (valid) step as the current step
      render @wizard.reload.current_step
    end
  end

The only other substantive change is the next / prev buttons in the views, which might look like this:

ruby
<%= form_for(@wizard, :url => wizard_path, :method => :put) do |f| %>
  <%= render 'show_errors' %>
  <div class="field">
    <%= f.label :address %>: <%= f.text_field :address %><br />
  </div>
  <div class="actions">
    <%= f.submit "back", :name => 'step' unless @wizard.first_step? %>
    <%= f.submit "next", :name => 'step' unless @wizard.last_step? %>
  </div>
<% end %>
Avatar

For running active_admin on Heroku add that line to your config/environments/production.rb.

ruby
config.assets.precompile += ['active_admin.js', 'active_admin.css']

And after that run rake assets:precompile, add public/assets to git, push to heroku and enjoy.

Also check that option in production.rb (should be false for heroku).

ruby
config.assets.compile = false
Avatar

Yes, i have the same problem but no solution.

Avatar

Hello,

I am trying to make a chat on my website looking like google talk. I
want to manage the disconnect event. A user can disconnect on several
way :

  • By clicking the disconnect button (easy to push the event to his friends)
  • By close his windows (I can send the event to windows close javascript event but if the user has many windows open on my website, i don't want to disconnect him.)

So, how can i detect when my connection on a channel is closed? Each
user has his own channel build with his id.

Thank you for help.

Avatar

who can help me?

class Attachment < ActiveRecord::Base
has_attached_file :att,
:url => "/files/:class/:id/:style/:basename.:extension",
:path => ":rails_root/public/files/systems/:id/:style/:basename.:extension"
end

class UserLogo < Attachment
has_one :user
has_attached_file :att,
:styles => { :original => "100x100>" } ,
:url => "/files/:class/:id/:style/:basename.:extension",
:path => ":rails_root/public/files/user_logos/:id/:style/:basename.:extension"

validates_attachment_content_type :att,
:content_type => ['image/png','image/jpg','image/bmp','image/jpeg','x-png','pjpeg']
end

Avatar

Ok found the issue. Apparently the gem ActiveAdmin (covered in a previous episode) causes conflict because it uses meta_search gem in the gemfile.lock, which makes Product.search use meta_search. A workaround to this is

Sunspot.search(Product) do

Avatar

I am using Rails 3.1 by the way if that matters

Avatar

First, thanks for the awesome tutorial!

I am running in to this error when I try to access the @search.results in my controller: undefined method `results' for #MetaSearch::Searches::Product:0x129d527d8

it seems like the @search always has a full collection of all the products no matter what string I pass it.

Here is how I declared searchable in my product.rb file

searchable do
text :title
end

and here is what my index action looks like in the products_controller.rb

def index
@search = Product.search do
fulltext params[:search]
end
@products = @search.results
respond_to do |format|
format.html
format.xml { render :xml => @products }
end
end

any idea why I keep getting that error?

Avatar

I'm wondering the same thing...saw you've asked this in a couple places on the web. Did you find an answer yet?

Avatar

After watching the video, I'm slightly confused. It seems that presenters more or less add another layer of complexity to the view concept. Is there some convention that if your controller and view are below a certain complexity level that a presenter isn't a best choice? When would I want to use a presenter vs leaving the show action with a little more data in it?

Also, are presenters main-stream or becoming main-stream? I've heard little about them until recently. Are they something that will most likely stick and become a standard convention?

Looks like something worth learning more about, but not sure I'm fully convinced they are very useful, but that's probably just my lack of experience with them speaking.

Avatar

So I see lots of people asking about roles and limiting what a specific user can see. Has anyone found a way to do this with active admin? At this point I'm not sure it's easily done.

Avatar

Great timing, I was just investigating the topic as I start witching to ruby 1.9 and wanted to keep that as painless as possible.

Can't wait for the deployment screencast now!

Avatar

Thanks for a great tutorial Ryan. I've noticed this method, when using Heroku, means that login details are case sensitive. I think it's because of the way Postgres works - has anyone found a way to adapt the code to allow login details to not be case-sensitive for Heroku?

Avatar

Looked over my code again and spotted an issue with my named route..... what a newbie!!!!!

Avatar

Got a site I'm working on and have followed the example above and got the authentication working except for the logout link, with the sessions#destroy not deleting the session and the redirect is going to the login page not the root_url.

Any help with this would be appreciated.

Avatar

+1 Really looking forward to the Capistrano version. I've been running Unicorn + nginx for a little while now and I still learned a bunch from this intro railscast. Thanks again!

Avatar

hm, that wasn't Ryan Bates, right?. Screencast outsourcing? :-)

Avatar

+1 For more deployment screencasts please! I'd be fascinated to see your deployment process.

Avatar

What if you wanted to order by using ajax?

Avatar

I know you posted this question 1 year ago, but it could be useful for some people.

RAND() is specific to MySQL, so if you are using PostgreSQL for instance it's random().

Avatar

But are there any clear instructions on how to do so George? Please see my question on SO: http://stackoverflow.com/questions/7867574/rails-3-1-1-http-streaming-with-apache-passenger

I'd appreciate it if you could point me to some good reference.

Avatar

I think ruby users on win32 (RubyInstaller & DevKit) will have to do "gem install ffi -v=1.0.9" before "gem install vagrant". There are issues with the current version of this virtualbox - dependency: https://github.com/ffi/ffi/issues/167

On the other hand the setup of vagrant on win64 runs well through jruby: http://vagrantup.com/docs/getting-started/setup/windows_x64.html