RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Just running through the ascii cast and it seems that you are missing curl from this list of things that need to be installed before rbenv install will run:

sudo apt-get install build-essential zlib1g-dev git-core sqlite3 libsqlite3-dev curl

Avatar

Added a link to the 'cast to the project README. Thanks, Ryan!

Avatar

Worth to mention that I had to add the 'type' options to make capybara work properly.

ruby
describe 'As a user I want to create a new account', type: :request do
  # ...
end

tags: paths NoMethodError undefined method 'visit'

Avatar

Fantastic! This could very much come in handy for future projects.

Avatar

Thanks!

It's just great!

By the way, I think there is a typo error on the Show Notes > Resources

Divise Warden Strategies (devise?)

Avatar

Tnx. That was very useful. I'm currently rewriting the authentication of production app and both of today's episodes have been exactly what I needed.

Avatar

You can also pass other options such as form in the initializer to override the login form. Like this:

provider :identity,
    form: lambda { |env| SessionsController.action(:new).call(env) },
    on_failed_registration: lambda { |env| UsersController.action(:new).call(env) }
Avatar

Thanks for doing this Ryan. Again, you have read my mind by creating this. Also, thanks to Intridea for kicking ass!

Avatar

i think rails should have an authentication and permission engine by default. Anyway sorcery authentication looks much more clean to me.

Avatar

Thanks Ryan for wonderful episode!

But I'm not sure that you did correctly by moving price and released_at to product.js. It would be better if you made this methods in price.rb model and when you render product to json you would set methods price and released_at to render json format.

Avatar

explicitly rendering the collection partial,

ruby
<%=render :partial=> 'product', :collection=>@search.products%> 

instead of the shortcut render @search.products

sorts the problem

Avatar

You can't really remove the default OmniAuth identity pages because they are part of the omniauth-identity gem. You just stop using them.

Avatar

Ryan,

Great screencast!

One thing though.. you did not illustrate how to remove the default OmniAuth identity pages once you've created your own.

-ra

Avatar

This is great, really great. When create an application with authentication, i used the
devise. But, after that i will try to integrate the warden. There is a very intresting
point that move our authentication up into Rack middleware. Great!

Every time our eyes keep on your screencasts.
Thank you Ryan,

Avatar

Nice Tutorial! I just want to post here a fix I discovered. Sorry if this would be a repost:

In your tutorial:
$('#products').html('<%= escape_javascript(render("products")) %>');

But in Rails 3.1, you have to explicitly define it as a partial:
$('#products').html('<%= escape_javascript(render :partial => "products" %>');

I hope this helps anyone who had the same issue. :)

Avatar

Ryan - first of all thanks a lot for doing this episode. But I'm having a few problems implementing this.

Avatar

Ryan - first of all thanks a lot for doing this episode. But I'm having a few problems implementing this.

Avatar

Hi Ryan!

Great Episode!

Is there any possibility you could make an episode on jCrop with Paperclip?

Avatar

Thanks Ryan for this awesome railscast....worked like a charm in my webapp. Is it possible to order the states by name?

Avatar

Please 'revisit' this for Pro subscribers!

The sortable list revised screencast was GOLD.... would really appreciate a Rails 3.1 working example of polymorphic associations for Rails 3.1....

Avatar

Guys anyone knows how to apply this sortable logic for associations.

For example, i have a table videos (id, name, video_category_id) and video_categories (id, name). I am displaying video name and video category name in a table. Now when someone wants to sort using video category name, it should sort records with video category name instead of using video category id from the association column in video table.

Avatar

I prefer Dragonfly, but I really like how you approached this. Great timing too!

Avatar

I get the following error:

SQLite3::SQLException: no such column: taggings.

Sadface.

Avatar

How can we make a validation about the token field? Just, I want to validate presence of this field. Any idea?

Regards

Avatar

(rails >= 3.1.1) When precompiling assets, shouldn't rails include both hashed and non-hashed version of the images?

Avatar

I'm using rvm and have managed to deploy 1 app with a certain degree of success. I'm now struggling to deploy another where my gems are defined in a gemset. Unicorn seems to be expecting them in the global space. Any idea what I can stuff in my unicorn.rb to tell it to use my gemset in this instance?

Avatar

Hey Ryan, great cast as usual. Karol's question above is still open, my Google searches thus far have not turned up any answer. Besides making a plugin, is there a place Rails 3 looks for custom generators outside of a Rails app as it did in Rails 2?

Avatar

For those wanting to use this with Rails 3:

  • Add the git link to your Gemfile [1]
  • Make sure you use <%= when using the calendar_for helper

[1] gem "table_builder", :git => "git://github.com/p8/table_builder.git" - do not rely on the released gem, it seems to be stale.

Avatar

Thanks for this Ryan.

In your 'how I test' presentation, you use factory_girl_rails and guard-respec. Where do they belong in this setup? The Gemfile refers to the gemspec file, so should they go in the gem spec file? And can you use instructions like ,

:require => false if RUBY_PLATFORM =~ /darwin/i

Avatar

I had all sorts of trouble getting the cropper changes to work. Firstly, you'll need to restart your Rails server every time you change the Paperclip changes.

Secondly, I couldn't really get the cropping code to work without putting quotes around the resize and cropping amounts. Because I always crop to a certain size, I completely replaced the array used in the transformation command.

ruby
module Paperclip
  class Cropper < Thumbnail
    def transformation_command
      if @attachment.instance.cropping?
        crop_command
      else
        super
      end
    end
    
    def crop_command
      target = @attachment.instance
      if target.cropping?
        sTransform = "#{target.crop_w.to_i}x#{target.crop_h.to_i}+#{target.crop_x.to_i}+#{target.crop_y.to_i}"
        sTransform = ('"'+sTransform+'"')
        trans = []
        trans = trans << "-crop" << sTransform
        trans << "-resize" << "\"800x200\""
        trans 
      end
    end
  end
end

Apologies for the Hungarian notation. Old programmer & bad habits die hard.

Avatar

This would be a good one to revise.

Avatar

especially if you put these into rspec let blocks:

ruby
describe Bla do
  let(:matt) { Factory.create :user { |f| f.name "Matt" } }

  it "should have a valid name" do
    matt.name.should eql('Matt')
  end
end
Avatar

This is almost eerie. I was just now Googling around for an in-place edit solution in Rails... and suddenly it shows up as one of the latest Railscasts :). Awesome!

Avatar

FYI - Devise no longer uses GET for the sign_out it uses DELETE. So you need to make your log out button/link use the DELETE method. Or change your route so to something like this which will use GET.

devise_scope :user do
get "/logout" => "devise/sessions#destroy"
end

Avatar

great as usual

how can edit grid data?

i wanna using it to admin panel

Avatar

Slightly OT question. Where did you get this comprehensive list of states/countries? I know many sources but they all require fair amount of work to import in rails db.

Avatar

The hack does fix it, thanks.

Avatar

Amaizing episode here. A railscast over cancellation/changing plans and other events like these that might happen would be awesome!

Thank you!

Avatar

Excellent tutorial! And a huge thank you to Jakub for the Rails 3.1 help!

Avatar

Also, don't forget that Rails.env is used instead of RAILS.ENV in rails 3!

Rails 3 do

ruby
APP_CONFIG = YAML.load_file("#{Rails.root}/config/config.yml")[Rails.env]

I also set up my config file with defaults that are loaded into each environment to keep things DRY.

ruby
defaults: &defaults

development:
  <<: *defaults

test:
  <<: *defaults

production:
  <<: *defaults
Avatar

This would be a good one to revise.

Avatar

Just noticed the same behaviour here, kinda weird.

Avatar

Wow, coming up to 3 years old and this is still a super easy way of doing sorting by dragging. I have just used this approach alongside your brilliant nested attributes tutorials (http://railscasts.com/episodes/196-nested-model-form-part-1) and it works just as well as the simple model example you use in this tutorial.

For me I have a Page model with nested attributes on a Snippet model (all very CMS based). If anyone was thinking of doing the same, go for it. I did this - it may help you:

1 - added a controller for the nested model - "Snippet" in my case (not needed otherwise) which just contains the "sort" method as you have it i.e.

ruby
class SnippetsController < ApplicationController

  def sort
    params[:snippets].each_with_index do |id, index|
      Snippet.update_all(["position=?", index+1], ["id=?", id])
    end
    render :nothing => true
  end

end

2 - add the drag n drop code to my show view rather than my index view like this:

erb
<ul id="snippets">
  <% for snippet in @page.snippets %>
    <%= content_tag_for :li, snippet do %>
      <span class="handle">[drag]</span>
      <%= snippet.body %>
    <% end %>
  <% end %>
</ul>
<%= sortable_element("snippets", :url => sort_snippets_path, :handle => "handle") %>

it helped me to keep the sorting feature out of the way of the nested attribute feature in the edit view for my parent model

3 - since i am Rails 3.0.x based i have added a default_order scope to my nested model to handle the sorting bit:

ruby
class Snippet < ActiveRecord::Base
  belongs_to  :page
  acts_as_list
  
  scope :default_order, order("position")
end

4 - for Rails 3 i have also used the routing approach added by Tyler Gannon in these comments i.e.

ruby
resources :snippets do
  collection do
    post :sort
  end
end

all works a treat. thank you for your excellent work as always Ryan!

glenn

p.s. the great thing about all this is that it just works without messing about! i was especially happy that the sort method only acts on those "id's" that i am currently working within my "show" view and NOT all objects i.e. it updates the positions for those snippets in my current page, not all snippets. the acts_as_list gem is optional for me in that my position attribute is optional but i may also end up with several snippets with NO positions by default so i'd sooner have positions added by the acts_as_list gem on new snippets - even if the calculated 'next position' is not scoped to my current page model. it's no big deal because as soon as you reorder your snippets it all sorts itself out!