RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

I am new to rails, and I love your casts. I've also tested this cast to my new project and it does not work from the text_field. If I use: http://lvh.me:3000/categories.json?q=ook

this url returns the category ok, but if i type "ook" to text field nothing is found. I also found that tokenize script transforms text to lowercase when using "like" SQL command.

i use:
$(function() {
  $("#product_category_tokens").tokenInput("/categories.json", {
    crossDomain: false,
    prePopulate: $("#product_category_tokens").data("pre"),
    theme: "facebook",
    minChars: 3,
    tokenLimit: 3
  });
});

Thanks for any help.

Avatar

I'm also interested in knowing the answer to @Gnago's question. Anyone?

Avatar

@1239: Check out episode 167 for an example of doing something similar. Maybe you could combine all these ideas to get what you want.

Avatar

I swear, it seems like every time I'm in the middle of a new project, you release a screen cast on the same subject. Please keep up the great work!

Avatar

Thanks for the screen cast. this is very helpful.

I have problem. I'm using
rails 3 ,
device 1.2.rc ,
token_authentication

I need to create a new object and save in the database through active resource.

book = Book.create(:name=>'abc',:auth_token=>'423423423423')

but this returned 401 error. (unautherized). if I removed the authenticate_user! command, then this will work.

but I need to create a row with auth-token. is there any one done this?.

please reply.

thanks
 

Avatar

Sorry maybe my previous comment was not clear enough (I am not native english speaker).

Maybe it will be easier if I say I want to add an author that is not already present in the list, it doensn't looks like possible with this plugin, am I wrong?

Can someone please suggest an alternative to achieve this result?

Thanks

Avatar

Thank you really useful episode.

I just have a question, what if I don't have a finite number of choices? Let's say I want to use the jQuery Tokeninput on a field where users can enter tags, so the app should add the new tag if it is not already present in the list. Is it possible?

Avatar

Instead of changing Rails' JSON default (or mapping to the attributes as shown), you could also check out the jsonContainer option of the Tokeninput plugin.

Avatar

Hi, I have followed the railscast to implement the solution using Redis as suggested as I thought it was brilliant :)
I am using redistogo.com as my redis server. But something I don't get is that the performance are DISASTROUS, I am talking minutes to load every single page. I thought you said that using redis would load the I18n store in memory ? Am I mistaken ? Because I think everytime transalt accesses the store it goes to redis... and obviously it takes times a bit of time for the connection, etc, but since I use it massively it simply kills the the performance of the site. I tried using the simple hash and this works just fine...

Alex

Avatar

Thanks guys for mentioning alternative solutions. I'll have to check those out.

@miguelsan, thanks! Code is added now.

@Prem, we can't use $(this) here because it is still the scope of the dom ready function. We don't have a new function, just a hash of options.

Avatar

Thanks for another very useful episode.

A small refactoring tip: I think inside the jQuery function you can use $(this) to reference the object.

// application.js
$(function() {
  $("#book_author_tokens").tokenInput("/authors.json", {
    crossDomain: false,
    prePopulate: $(this).data("pre"),
    theme: "facebook"
  });
});

Avatar

As usual great work. Thanks. :-)

FYI Ryan, rather than doing product.versions.scoped, I think you can use product.versions.reload (I think the passing true to the scope/relation is getting phased out in favor of reload, which I think is better).

Avatar

Very useful. Keep them coming on Rails/jQuery theme now that 3.1 will ship with jQuery as the default.
Bharat

Avatar

Dang.....I needed this episode two weeks ago. Maybe I'll revisit my crappy solution with something more like this.

Avatar

Really nice interface.

I've also used a couple more jQuery solutions for multiple selection:

* Bsmselect: https://github.com/vicb/bsmSelect
* Multi select: https://github.com/michael/multiselect/

These two solutions have advantages over tokeninput:

* No need to change server code, nor to implement any search actions.
* They work without javascript as well, just as a normal multiple select.

On the other hand, it looks to me tokeninput is very user friendly. That gives me an idea to combine the advantages of both worlds...

Thanks!

Avatar

This is a very nice solution when the number of entries is small. But when that number grows and/or you are not sure what options are available, a good alternative is to use two select menus in tandem. You populate the second from the first and can fine tune that list before submitting.

There are a number of jQuery solutions for doing this. I just posted one last week and hope to have an example Rails helper in the next day or two.

http://craic.github.com/tandem_select_jquery_plugin/

Avatar

By the way, I think that you forgot to write down the model code.

Avatar

Glad to get to know this cool autocompleting plugin, much more beautiful and user oriented than any of the previous ones that I have seen, I guess.
Thanks for showing us how to use it easily, and thanks for all your wonderful work during all the past 258 weeks.

Avatar

I am trying to do this screencast as a form of update to an image not as an upload like the screencast shows... it returns saying it worked but the thumbnails have not changed...

I have :small, :medium and :large i want the :small and :medium to change first and leave :large and orginal

Any ideas why this may not be working? is there a particular version of paperclip required? Imagemagick etc? or should i modify the code in a particular way? any help much appreciated.

Avatar

Ryan (or anyone): One question: The client is effectively doing a GET, right? And GETs are supposed to be idempotent (not change the state on the server). Do you have to do anything special to tell the server's cacheing mechanisms that a GET on the same URI twice in a row might yield different results? Or is it just luck that the &after= query parameter has that effect (since it changes continually)?

Avatar

Hi everyone wonderful episode,
 but a small problem i'm facing that is
i have a has_many association set up between category and sub category,
Class Category ..
 attr_accessible :name, :subcategories_atttributes
 has_many subcategories, dependent => :destroy
accepts_nested_attributes_for :subcategories

end

Class Subcategory..
belongs_to :category

end
 
error which i get while saving a category along with its subcategories is
    * Subcategories category can't be blank

thanks in advance

 

Avatar

ok, update on my last comment, I neglected to remove the cookies that were previously there, everything is now functioning smoothly, thanks!

Avatar

I'm having a really hard time getting this to work with devise to share cookies and authentication info between sessions, has anybody had this type of problem before?

thanks!

Avatar

For the AJAX version,

you add a ":remote => true" argument to the paginate() call from index.html, then add something like this to the index.js.erb:

https://gist.github.com/876500

No application.js hooks needed, with unobtrusive JS, since any link with data-remote already has a .live() hook attached to it.

Avatar

Kaminari ajax pager, details in gist:

https://gist.github.com/876500

No extra application.js hooks needed.

Avatar

@Ryan
I was able to use the object_ids method even in has many through though the video says that i have to create my own method for HMT. Can you clarify this if possible?. I am on Rails 2.3.5 and Ruby 1.8.7.

Thanks,
Shashank

Avatar

Of course you could do one with Sunspot....hopefully using mongoid

Avatar

A good mongoid cast would fit in nicely...of course would need xmlpipe

Avatar

Ryan

For some reason I am having trouble mapping this to mongodb using mongoid. I believe this portion sis the culprit..

if search
    find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
    find(:all)
end

Is the problem with the LIKE clause?

Avatar

authentication information have changed to the omniauth.auth parameter of the Rack environment. simple change:

auth = request.env["rack.auth"]

to

auth = request.env["omniauth.auth"]

Avatar

Very interesting alternative to Cucumber. Reminds me of a new feature in Capybara that integrates RSpec acceptance tests, letting you write a test like this:

require 'acceptance/acceptance_helper'

feature "Articles", %q{
  In order to have an awesome blog
  As an author
  I want to create and manage articles
} do

  background do
    Article.create!(:title => 'One')
    Article.create!(:title => 'Two')
  end

  scenario "Article index" do
    visit '/articles'
    page.should have_content('One')
    page.should have_content('Two')
  end

end

For more info, see this excellent blog post: http://jeffkreeftmeijer.com/2011/acceptance-testing-using-capybaras-new-rspec-dsl/

Avatar

You saved me hours of digging around. Thank you a billion trillion thank-you-based-thingers.

Avatar

Does anyone have any thoughts for implementing this on an edit or destroy multiple action?

Avatar

Here's the cludge I used:

    if params[:search].nil?
      params[:search] = Hash.new
    end
    if params[:search][:meta_sort].nil?
      params[:search][:meta_sort] = "my_sort_field.desc"
    end

Avatar

Anyone figure out yet now to set a default sort (where one not specified)?

Thanks Ryan.

Avatar

As some have noted,

yield(:title) || "Default"

no longer works in Rails 3. I recommend adding this method to a Helper:

def yield_or_default(section, default = "")
    content_for?(section) ? yield(section) : default
end

and then calling

yield_or_default(:title, "Default")

Thanks for the great work, Ryan.
--Ben

Avatar

is it just me, or does the question mark on the function name break this?

This works for me:
validates_presence_of :state, :if => :in_us
def in_us
  country == 'US'
end

This, would not:
validates_presence_of :state, :if => :in_us?
def in_us?
  country == 'US'
end

Avatar

FYI.
To uninstall simple_form from your project, comment gem 'simple_form' in your Gemfile and run this commands:
rails destroy simple_form:install
bundle

Avatar

Do I need to use require statement in rails to use this gem. When I just do gem lorem it loads but the lorem.rb module is not available. please help

Avatar

(sorry 'bout my weird sign-in ID.. not sure how to change it!)

Since engines are intended to be 'portable' between applications, the suggested strategy for handling the engine's migration files by copying may pose problems, as it'll always carry the encoded timestamp of the date/time when it was created.

Maybe the the timestamp prefix should be changed when copying to the 'current' (at the time of copying) timestamp prefix. Do you see any issues with that?

Avatar

Incredible amount of information passed on!
I'd loved to see you implementing the link_to_function in the view before you wrote the spec for it and having it fail. I know you would have had two failures, firstly the problem with the missing feature and secondly the problem with the missing :js => true. I understand that for a cast, as packed as this one, that might have been too much.
However that is just so much the workflow, that happens to me all the time.

Hmm after all, maybe this cast, which I still consider one of the greatest for my own interest and benefits, might have been worth being split into two....

But I appreciate that great balance of simple, intermediate and advanced casts.

Thank you for the great work
Robert

Avatar

Alright, I figured out the 406 problem for other people who run in to it.

This is not a server-side issue, but rather an error in a presumption made in this example.

Odds are you're implementing this for a controller that already exists, and it already has a "def index" section.

For example, my code already had this:

# GET /users
# GET /users.xml
def index
 @users = User.all

 respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @users }
end

The 406 is occurring because this def index doesn't have a way to respond to javascript requests.

So after the format.xml line, add this:

format.js { @categories = Category.find(:all, :conditions => ['name like ?', "%#{params[:search]}%"]) }

VIOLA!

Avatar

I love your screencasts, thanks a lot !

Avatar

Several other users have mentioned it, and there has been no resolution posted.

When I go to http://localhost/categories.js, as shown in the demo, nothing is listed and a 406 error is returned in the logs.

Can someone provide a solution for this?

Avatar

Hey /users/1005 you could read up a cool book "The Rails way" it has got some nice insight on use of nested models and resources... :)

Avatar

Awesome, as usual.

The only thing I'd say is that the action is misnamed. You're not reverting the version in the parameter - you're reverting TO that version.

Avatar

Is this significantly faster than Cucumber, or is it purely an aesthetic decision?