Rails forms

How to implement data ordering and importing/exporting?

Here’s the situation in the context of shovell application

Let’s say we need to add new stories. To make it more effective we could use importing.
The stories:
zip The story teller, http://www.zip.com/story_teller/
zip The fantastic adventure, http://www.zip.com/fantastic_adventure/
zip Summer holidays, http://www.zip.com/summer_holidays/
zip Space travel, http://www.zip.com/space_travel/

The reverse would be nice too (exporting).

Next: it would be good to sort the data.
Much like fetch_stories method in stories_controller.rb. How to display stories that start with “zip”. I know how I would do that in PHP. I would make SQL statement “select * from stories where name like ‘zip%’”. And of course HTML select tag to select stories where the link is for a particular domain like “http://www.zip” (this would be the sort by unique value filter).
Then the other: ordering. A link over columns to order data ascending or descending. Should the direction be stored in session hash?

It seems like a basic thing but it’s not. I don’t know how to operate forms in rails well enough as I do in PHP.

Thanks in advance!

Absolutely. Instant Rails is more trouble than it is worth. It is very easy to install the elements separately, and far more reliable.

I’m not sure what you mean by “importing” and “exporting.” Could explain that a little more fully and then maybe I can help.

As far as sorting stories that could be fairly easy depending on how you want to do it. There is a gem called Searchlogic that is excellent for searching and includes functions for sorting as well. http://github.com/binarylogic/searchlogic

Think of the data input mentioned in the starting chapters of the book.

Open irb console, type information manually like:
s=new Story
s.name=“some story”
s.link=“http://”

Then a form was introduced to add a story. But you add each story separately.
So, how to expand this functionality to add many stories.

What I have in mind is a link to populate database with test data to add a bunch of stories just to test the functionality.
Sure I could use a database client but I haven’t dealt with sqlite3 DBMS. I use SQLyog for MySQL.

Thanks for the information, very useful, if you have more information please tell us.

If you are wanting to put data in simply to test it, I would recommend creating a migration to populate the test data. I would also recommend getting comfortable with fixtures, unit tests, and the functional tests that are part of the Rails core since these will help you find bugs much more efficiently than populating a db and clicking on everything to see if it works.

Tests need to be re-written if changes occur in the application. I am used to manual tests. When I went along the simply rails 2 2nd edition book tests were the part that I failed at. I didn’t use the code archive and typed it from the book. I did make some spelling mistakes but I couldn’t get the tests to work so I gave up because the rest of shovell was functioning like expected.

Maybe in time but for now I would be grateful if you could demonstrate a sample of how that migration file would look. I need test data for demonstration purpouses so that I can show to the audience an empty database, then not to waste time load some test data and demonstrate again that this data is now available.

Oh man

If you want to “seed” your database, Rails provides a way to do it. http://railscasts.com/episodes/179-seed-data Not sure if you are familiar but the Railscasts are excellent for learning about Rails, there may even be something about adding multiple records at once.

It would be even better if it worked

C:\\InstantRails\\rails_apps\\shovell>rake db:seed
(in C:/InstantRails/rails_apps/shovell)
rake aborted!
Don't know how to build task 'db:seed'

(See full trace by running task with --trace)

C:\\InstantRails\\rails_apps\\shovell>

:sick:

What version of rails are you running? Anything lower than 2.3.4 doesn’t have the “seed” function.

Rails version

C:\\InstantRails\\rails_apps>rails -v
Rails 2.3.5

shovell\db\seeds.rb

puts "rails seed database"

Hmmm…that is weird. I just tried it on a fresh 2.3.5 install on Instant Rails and it worked.

Perhaps your version of rake is off? You can check what version you have by running gem list

and if rake needs to be updated you can run: gem update rake

Let me thank you for the help so far. I appreciate your cooperation.

Here’s the output of gem list and gem update rake

C:\\InstantRails\\rails_apps>gem list

*** LOCAL GEMS ***

actionmailer (2.3.5, 2.0.2)
actionpack (2.3.5, 2.0.2)
activerecord (2.3.5, 2.0.2)
activeresource (2.3.5, 2.0.2)
activesupport (2.3.5, 2.0.2)
capistrano (2.5.10, 2.1.0)
cgi_multipart_eof_fix (2.5.0)
cmdparse (2.0.2)
columnize (0.3.1)
fxri (0.3.7, 0.3.6)
fxruby (1.6.12)
gem_plugin (0.2.3)
highline (1.5.1, 1.4.0)
hpricot (0.8.2, 0.6)
linecache (0.43)
log4r (1.1.2, 1.0.5)
mongrel (1.1.5, 1.1.2)
mysql (2.8.1, 2.7.3)
needle (1.3.0)
net-scp (1.0.2)
net-sftp (2.0.4, 1.1.0)
net-ssh (2.0.16, 1.1.2)
net-ssh-gateway (1.0.1)
rack (1.0.1)
rails (2.3.5, 2.0.2)
rake (0.8.7, 0.8.1, 0.8.0, 0.7.3)
rubygems-update (1.3.6, 1.3.5, 1.0.1)
searchlogic (2.4.19)
sources (0.0.1)
sqlite3-ruby (1.2.5, 1.2.1)
win32-api (1.0.4)
win32-clipboard (0.4.3)
win32-dir (0.3.2)
win32-eventlog (0.4.6)
win32-file (0.5.4)
win32-file-stat (1.2.7)
win32-process (0.5.3)
win32-sapi (0.1.4)
win32-sound (0.4.1)
windows-api (0.2.0)
windows-pr (0.7.2)

C:\\InstantRails\\rails_apps>gem update rake
Updating installed gems
Nothing to update

C:\\InstantRails\\rails_apps>

Maybe all of this has something to do with rails enforcing the use of version 2.0.2. Some step in the book. I don’t know.

Ahhh! That would be the problem! In your environment.rb file you should have a line that looks like this:

RAILS_GEM_VERSION = '2.0.2' unless defined? RAILS_GEM_VERSION

This is telling the system to use Rails 2.0.2. (which did not have the db:seed feature) You simply need to change it to this:

RAILS_GEM_VERSION = '2.3.5' unless defined?  RAILS_GEM_VERSION

Also, something to keep in mind is that if you run gem cleanup, it will remove old versions of gems that you have on your system. Run it like this: gem cleanup -d For a “dry run” to see exactly what it is going to remove and then run gem cleanup if you want to actually remove old gems.

Still no luck. Maybe I should just re-install everything but this time without instant rails. Ruby, then RubyGems and so on. It would be nice if there was a way to get rails working with wAMP.

Output

C:\\InstantRails\\rails_apps\\shovell>ruby script/server
=> Booting Mongrel
=> Rails 2.3.5 application starting on http://0.0.0.0:3000
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/rails/gem_dependency
.rb:119:Warning: Gem::Dependency#version_requirements is deprecated and will be
removed on or after August 2010.  Use #requirement
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/initializer.rb:602:i
n `send': undefined method `cache_template_extensions=' for ActionView::Base:Cla
ss (NoMethodError)
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/initial
izer.rb:602:in `initialize_framework_settings'
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/initial
izer.rb:601:in `each'
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/initial
izer.rb:601:in `initialize_framework_settings'
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/initial
izer.rb:598:in `each'
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/initial
izer.rb:598:in `initialize_framework_settings'
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/initial
izer.rb:155:in `process'
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/initial
izer.rb:113:in `send'
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/initial
izer.rb:113:in `run'
         ... 6 levels...
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/command
s/server.rb:84
        from C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require
.rb:31:in `gem_original_require'
        from C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require
.rb:31:in `require'
        from script/server:3

C:\\InstantRails\\rails_apps\\shovell>