Ruby

Lee Hughes
Lee Hughes
8,380 Points

Best practices for importing a csv file for Select2

I have downloaded the example app provided https://github.com/jseifer/rails-select2-example and have started to play around with it. I really love the feature but I need it to use a country drop down with corresponding cities.

I have found a csv of countries https://github.com/railscasts/088-dynamic-select-menus-revised/blob/master/store-after/countries.csv

as well as states https://github.com/railscasts/088-dynamic-select-menus-revised/blob/master/store-after/states.csv

What would be the best way to integrate these two together?

I'm researching using an import (require) feature and also trying to use the name,abbreviation,country_id which are referenced on the csv somehow.

If anyone can point me in the right way it would be most appreciated.

19 Answers

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hello Lee Hughes

I've put a couple of videos together to help you out. I answered your question in two parts, Seeding Databases with CSV Files...

...and Updating Select2 Drop-downs via AJAX.

For those who are interested in following along with parts 1 and 2 you can download the files here.

Whilst you did ask for how to import CSV files for countries and states, you may be able to find a Gem that would take care of that aspect of your project however I showed the CSV approach like you asked for as it may be helpful in other seeding scenarios.

Regards,
Andrew

Jamie Barton
Jamie Barton
14,498 Points

Thanks for this! It's great!

is this always going to b available? i dont have time to follow it now

Andrew Chalkley
Andrew Chalkley
Treehouse Guest Teacher

Yep, this will be here when your ready to follow along.

Lee Hughes
Lee Hughes
8,380 Points

Andrew Chalkley This is amazing! Thank you so much for taking the time to do this!

Andrew Chalkley
Andrew Chalkley
Treehouse Guest Teacher

No worries. Thank you for studying with us!

Tedd Arcuri
Tedd Arcuri
8,058 Points

I agree. It's awesome that you guys do stuff like this!

hey guys can someone help me out here, i have been stuck on this for long noe, am running rails 3.2.16 and i am trying to generate my first status on the rails dev track here at treehouse and i have the following problem,thanks

When generating my first status, that is when I do the localhost/statuses I have the following error:

ExecJS::RuntimeError in Statuses#index

Showing C:/Users/NEBELYN/ruby1/gopbook/app/views/layouts/application.html.erb where line #6 raised:

(in C:/Users/NEBELYN/ruby1/gopbook/app/assets/javascripts/statuses.js.coffee) Extracted source (around line #6):

3: 4: Gopbook 5: <%= stylesheet_link_tag "application", :media => "all" %> 6: <%= javascript_include_tag "application" %> 7: <%= csrf_meta_tags %> 8: 9: Rails.root: C:/Users/NEBELYN/ruby1/gopbook HERE IS MY GEM FILE

source 'https://rubygems.org'

gem 'rails', '3.2.16'

Bundle edge Rails instead:

gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'

Gems used only for assets and not required

in production environments by default.

group :assets do gem 'sass-rails', '~> 3.2.3' gem 'coffee-rails', '~> 3.2.1'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes # gem 'therubyracer', :platforms => :ruby

gem 'uglifier', '>= 1.0.3' end

gem 'jquery-rails'

To use ActiveModel has_secure_password

gem 'bcrypt-ruby', '~> 3.0.0'

To use Jbuilder templates for JSON

gem 'jbuilder'

Use unicorn as the app server

gem 'unicorn'

Deploy with Capistrano

gem 'capistrano'

To use debugger

gem 'debugger'

THAKNS GUYS

Adam Faur
Adam Faur
2,365 Points

@Pavey Nganpi Typically that means that need to gem install therubyracer however it looks like you have that installed so heres my stab at it..

I saw in your gemfile you are using...

gem 'therubyracer', :platforms => ruby

Have you tried changing your gem file to read:

gem 'therubyracer'

Then running bundle install.

Also, as a test you might try running:

bundle exec rake assets:precompile --trace

and see if you have any errors.

I don't use windows and don't know if you are, but I saw some things indicating that using the , :platforms => ruby does not work in some instances. I have included sources below for more information. Hopefully this helps, but if not I hope that Andrew might be able to shed some light on this one as well.

http://stackoverflow.com/questions/8362458/error-when-running-rails-app-execjsruntimeerror http://www.railszilla.com/rails-3-coffescript-execjs/rails http://stackoverflow.com/questions/8421321/does-using-platforms-in-your-gemfile-work

thanks for the reply, i did try with gem rubyracer and nothing changed, so i tried the one above, still nothing, also when i try rubyrace and run install bundle it runs and gives an error saying i have to intall libv8, which i tried to install by running install libv8 --version"3.16.12.14". this ran and then gave an error saying it cant find the path, i have been on this pb for the past 2 days, i feel discouraged about learning rail

Adam Faur
Adam Faur
2,365 Points

@Pavey Nganpi Don't be discouraged! Many have been right there in your shoes before.
It's a constant challenge to overcome a new problem but don't let it get you down.
Remember, confusion is the very first step of learning and this is the really cool stuff, so, it is usually the most confusing =).

I would like to recommend that you look at some more resources which I included at the bottom of this message.

Based on your last answer you may need a solution to install libv8 and since it is not working (by installing therubyracer) you might try installing nodejs to solve this dependency.

You can find a windows version of the installer here:

http://nodejs.org/

The short answer is since nodejs uses libv8 exclusively as its engine you will get it by installing nodejs.

Based on what some other people are saying you may be able to install rails from here to get libv8 but since you already have rails installed it might just confuse things further. (RailsInstaller.org)

http://stackoverflow.com/questions/6356450/therubyracer-gem-on-windows http://stackoverflow.com/questions/14823975/cant-install-libv8-gem-on-cygwin

thanks adam, i appreciate

Adam Faur
Adam Faur
2,365 Points

@Pavey Nganpi No problem.

Iban Dominguez
Iban Dominguez
17,973 Points

This is amazing, thanks tree house!

Adam Eury hi Adam, when i download and install node,do u know how i can integrate it into rails,thanks

Adam Faur
Adam Faur
2,365 Points

@Pavey Nganpi

Once you install node, you should now be able to remove:

gem 'therubyracer'

from your gem file, then run bundle install

By having nodejs installed you should now have libv8.

You should not need therubyracer any longer and by keeping it you might encounter another error.

ok thanks

@adam faur hey, Adam,since i have node installed and its the javacript engine for rails, is it possible to build stuff in my rails app using node.js? thanks

Adam Faur
Adam Faur
2,365 Points

pavey nganpi

Not that I know of..

The reason it was a requirement for your rails project was to allow the .coffee files to compile into .js.

If you find out something special you can do let me know =)

yh sure i will, :)

Adam Faur hi, i followed exactly what was explained in the rails4 todo lists tutorial here at treehouse on how to install rpec, but i didnt have the directories helpers,views, controller, and the rest, pls how can i generate theses directories,thanks

Adam Faur
Adam Faur
2,365 Points

I am really not sure on this Pavey Nganpi, you might want to ask about this in the forum.

I have not taken the course you are referring to, however, you should have these directories when running the rails new command.

When you post in the forum you should be very detailed, for example, you should indicate in your post the following things about your scenario:

  • What operating system you are running (windows/mac/linux and version (win7, win8, lion, mlion, ubuntu, arch)
  • What version/build of ruby and rails you are running (ruby 1.9.3, ruby 2.0, ruby 2.1, rails 3.2.16, rails 4.0, etc)
  • A gist, or pastebin of your Gemfile linked in also helps other people get a better idea of your situation.

You should be able to create these directories manually, and add files into them. Rails will pick up these files when your application runs. (If you are struggling to move forward you might try doing so to continue your progression.)

ok thanks adam