Placing Multiple Button_To on the Same Line – Ruby on Rails

This is a problem that had me confused for an embarrassingly long time. Rails wraps button_to elements within a form and a div. Unfortunately this will take up 100% of the available width. Thankfully the solution is pretty straight forward – simply wrap buttons in a div and float them left like so:

    
'button'%>
'button' %>

A wrapper div isn’t strictly necessary however I usually add one just to be safe. While this will probably give a bunch of designers another reason to hate developers, I find that wrapping floated elements seems to save a lot of headaches when making changes down the track.

Using the code above you may also find that the divs aren’t filling as expected, simply add a div with a ‘clear:both’ style to the bottom of the wrapper:

'button'%>
'button' %>

Hopefully that doesn’t take anywhere near as long for you guys to figure out as it did me, time for a coffee break I think. Good luck!

Routing Error – No Route Matches [Post] “orders/new”

I ran into a fairly common routing error this morning, thankfully these are fairly easy to fix – usually!

Routing Error
No route matches [POST] “/orders/new”

Simply add the following the route to your routes.rb file:

siteconfigroutes.rb

#Orders
controller :orders do
post ‘orders/new’ => ‘orders#new’
end

You may also have to restart WEBrick for this new route to take affect. Good Luck!

How to View a Table’s Structure – Sqlite3

Just a quick post on how to view a table’s structure in SQLite3. Again, not something I’ve broken yet – more something I seem to keep forgetting. Simply start Sqlite:
chris@chris-VirtualBox:~/site$ sqlite3 -line db/development.sqlite3
SQLite version 3.7.4
Enter “.help” for instructions
Enter SQL statements terminated with a “;”

Then enter the following: – substituting orders for whatever your table name happens to be:
sqlite> pragma table_info(orders);
cid = 0
name = id
type = INTEGER
notnull = 1
dflt_value =
pk = 1

cid = 1
name = user_id
type = integer
notnull = 0
dflt_value =
pk = 0

cid = 3
name = created_at
type = datetime
notnull = 0
dflt_value =
pk = 0

Good Luck!

View All Tables – Sqlite3

Not really something that I’ve broken (yet), but definitely something I found useful when starting out with rails – how to view all tables:

Start Sqlite3 i.e.
chris@chris-VirtualBox:~/site$ sqlite3 -line db/development.sqlite3
SQLite version 3.7.4
Enter “.help” for instructions
Enter SQL statements terminated with a “;”

Then simply enter the following:
sqlite> .tables
carts message_recipients products taggings
categories message_statuses schema_migrations tags
comments messages store_pages user_settings
images order_items store_settings users
line_items orders stores
locations product_images sub_orders
sqlite>

Good Luck!

Rails Server – Address Already in Use

Well, looks like the first thing I’ve managed to break today is WEBrick – a web server for ruby on rails. The general idea behind the error message appears to be that the address it’s trying to start is already in use:

[2011-12-02 19:01:34] INFO  WEBrick 1.3.1
[2011-12-02 19:01:34] INFO  ruby 1.8.7 (2010-08-16) [i686-linux]
[2011-12-02 19:01:39] WARN  TCPServer Error: Address already in use – bind(2)
Exiting
/usr/lib/ruby/1.8/webrick/utils.rb:73:in `initialize’: Address already in use – bind(2) (Errno::EADDRINUSE)
from /usr/lib/ruby/1.8/webrick/utils.rb:73:in `new’
from /usr/lib/ruby/1.8/webrick/utils.rb:73:in `create_listeners’
from /usr/lib/ruby/1.8/webrick/utils.rb:70:in `each’
from /usr/lib/ruby/1.8/webrick/utils.rb:70:in `create_listeners’
from /usr/lib/ruby/1.8/webrick/server.rb:75:in `listen’
from /usr/lib/ruby/1.8/webrick/server.rb:63:in `initialize’
from /usr/lib/ruby/1.8/webrick/httpserver.rb:24:in `initialize’
from /usr/lib/ruby/gems/1.8/gems/rack-1.3.4/lib/rack/handler/webrick.rb:10:in `new’
from /usr/lib/ruby/gems/1.8/gems/rack-1.3.4/lib/rack/handler/webrick.rb:10:in `run’
from /usr/lib/ruby/gems/1.8/gems/rack-1.3.4/lib/rack/server.rb:265:in `start’
from /usr/lib/ruby/gems/1.8/gems/railties-3.1.1/lib/rails/commands/server.rb:70:in `start’
from /usr/lib/ruby/gems/1.8/gems/railties-3.1.1/lib/rails/commands.rb:54
from /usr/lib/ruby/gems/1.8/gems/railties-3.1.1/lib/rails/commands.rb:49:in `tap’
from /usr/lib/ruby/gems/1.8/gems/railties-3.1.1/lib/rails/commands.rb:49
from script/rails:6:in `require’
from script/rails:6

 

Sure enough, it turns out that one of my previous WEBrick processes had not closed properly. I was able to view the existing processes using the following:
chris@chris-VirtualBox:~/site$ pgrep ruby
1601

 

In order to close them simply kill the process:
chris@chris-VirtualBox:~/site$ kill 1601

 

Ensure that the process is gone:
chris@chris-VirtualBox:~/site$ pgrep ruby

 

And finally, start WEBrick again.
chris@chris-VirtualBox:~/site$ rails server
=> Booting WEBrick
=> Rails 3.1.1 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2011-12-02 19:13:21] INFO  WEBrick 1.3.1
[2011-12-02 19:13:21] INFO  ruby 1.8.7 (2010-08-16) [i686-linux]
[2011-12-02 19:13:26] INFO  WEBrick::HTTPServer#start: pid=1814 port=3000

 

Good luck!