rails

start server

SOFTWARE: rails

ruby script/server lighttpd

fire up a rails server. run this command when in the directory of your rails app.

MORE INFO:

update search index

SOFTWARE: rails

ruby script/indexer

update simplesearch index for rails (run from inside your app's directory)

MORE INFO:

text area style

SOFTWARE: rails

<%= text_area 'record', 'reason', :style => "width: 400px; height: 200px" %>

explicitly specify the style of a text area in rails

rails
<%= text_area 'record', 'reason', :style => "width: 400px; height: 200px" %>
MORE INFO:

default blank date for date_select

SOFTWARE: rails

<%= date_select(:record, :reviewed_at, :include_blank => 1 %>

let's you designate a blank value as the default for a date_select in rails.

rails
<%= date_select(:record, :reviewed_at, :include_blank => 1 %>
MORE INFO:

destroy children

SOFTWARE: rails

has_many: comments, :dependent => :destroy

Code in the model to destroy children if the parent is destroyed. In other words, make sure you're not left with orphan records.

ruby
has_many: comments, :dependent => :destroy
MORE INFO:

generate scaffold

SOFTWARE: rails

script/generate scaffold Item Item

Generate a scaffold based on a given table in the db in rails. The example would generate a model and controller as the uppercase single (Item) named version of the related lowercase plural (items) table in the database.

MORE INFO:

clear logs

SOFTWARE: rails

rake log:clear

clear the logs. usefull to make your app smaller if you're moving it around between dev instances and things.

MORE INFO:

create migration

SOFTWARE: rails

script/generate migration Rating

Create a migration file in rails.

MORE INFO:

restart rails on site5 server

SOFTWARE: rails

skill -9 -u username -c dispatch.fcgi

Use this command to restart fast cgi processes on site5 hosted servers. Insert your username in place of 'username' in the example.

MORE INFO:

generate rails docs

SOFTWARE: rails

rake appdoc

generate documentation for your rails app. I've also seen this as "rake doc:app". Also this might be useful at some point to generate docs for plugins: "rake doc:plugins". Apparently you can rebuild docs with "rake doc:reapp".

MORE INFO:

rails deployment

SOFTWARE: rails

deployment notes on for rails at site5

I ended up freezing rails to the vendor directory by using

rake freeze_gems

I also froze all third party gems (search etc) by using the great script here:

This required a bit of tweaking to make sure that files that used to call a gem directly would now instead reference the appropriate file in the /lib directory. (freeze gems puts rails in the vendor dir, but freeze other gems puts them into the /lib directory.)

General tutorials for deployment at site5

MORE INFO:

rails orphans

SOFTWARE: rails

at the model level

Make the parent set to destroy dependent children if the parent is destroyed. Add this to the parent's model.rb file:

ruby
has_many :children, :dependent => :destroy

So it would look something like:

ruby
class Parent < ActiveRecord::Base
has_many :children, :dependent => :destroy
end

set alert at controller level for users

Found this code online. Could be used to set a flash alert to notify users that they can't destroy something because it has children ...

ruby
<at> company = Company.find(params[:id])
if company.contacts.size > 0
flash[:notice] = 'This company has contacts.'
redirect_to :action => 'show_company', :id => <at> company.id
else
<at> company.destroy
redirect_to :action => 'list_companies'
end
MORE INFO:

rails ratings

SOFTWARE: rails

star rating system notes

Tutorial here:

naffis star rating tutorial

software: rails

subject: order options list

Was having some issues trying to get options in a selection form list to come out the way I wanted. Found that the method:

find_all 

is set, whereas the more generic method find can be modified like so to provide some additional details:

find(:all, :order => 'name')
MORE INFO:

rdoc documentation

SOFTWARE: rails

You can run rake appdoc to generate automatic documentation for your rails app. There are two main areas to add documentation to your app. One is a general description which gives you a main doc page. The other is inline in your code via comments which is useful for describing details of how specifics are set up.

The Main Doc Page

To add general notes about your rails app to the auto-generated documentation, add them to the file doc/README_FOR_APP. The contents of this file get sucked into the generated documentation when you run rake appdoc from the shell. Your documentation gets created in doc/app with an index page at doc/app/index.html. Any content that you place in doc/README_FOR_APP shows up in the main frame of the index page for your documentation.

Use the Styling guidelines listed below to markup doc/README_FOR_APP and you will end up with a nicely formatted main page for your documentation. This is a great place to add the general info regarding your app.

Inline Code Documentation

To add documentation for classes and methods in your app, add comments (comments start with the pound sign in your code). The placement of the comments determines where they'll show up in the documentation.

To add a description for a class: add comments ABOVE the class declaration in your code.

\#Here is some documentation for the class
class MyController < ApplicationController

To add a description for a method: add comments ABOVE the method declaration in your code.

\#Here is some documentation for the method
def my_method

Styling the Documentation

headings are equal signs (as in alternate MarkDown syntax)

lists (note the period after a digit - req'd)

\* or - or digit.

paragraphs: consecutive lines of type

code: indent (as in MarkDown syntax)

markup

<b></b> etc

definition lists

[term]  definition
MORE INFO:

order options list

SOFTWARE: rails

Was having some issues trying to get options in a selection form list to come out the way I wanted. Found that the method:

ruby
find_all

is set, whereas the more generic method find can be modified like so to provide some additional details:

ruby
find(:all, :order => 'name')
MORE INFO:

date time format

SOFTWARE: rails

object.created_at.strftime("%a, %b %d '%y")

Use the strftime method to format date-time the way you like. In the nugget I'm assuming that 'object' has a 'created_at' column in the database so I'm running strftime on that to make it look pretty. You could also just run on something like this:

ruby
Time.now.strftime("%a, %b %d '%y")

Terry Donaghe has published a nice list of available arguments here:

rubynoob

MORE INFO: http://www.rubynoob.com/articles/2006/08/31/how-to-do-friendly-date-formatting-in-ruby

foreign key search

SOFTWARE: rails

add method to model to define foreign searchable field

Here's how I made a foreign key table value searchable with simple search in rails.

First make up a method name for the thing you want searched in the foreign key table and add to model "make_searchable" line:

ruby
make_searchable [ :name, :example, :description, :foreign_value ]

Then, define "foreign_value" later in the model so make_searchable knows what to look for:

ruby
def foreign_value
foreign.name
end

This assumes you have some foreign key table (foreigns) and it has a column called "name" that you're accessing.

Of course you'll need the proper relationship between the tables in the model like:

ruby
belongs_to: foreign

But you'll probably have that in your model already.

This was a simple extension of the idea provided in the comments of the post in the ref url for this cheat - /bin/recykl- (it was for ferret instead of simple search.)

MORE INFO: http://blog.zmok.net/articles/2006/10/18/full-text-search-in-ruby-on-rails-3-ferret

undefined method 'require_gem'

SOFTWARE: rails

change 'require_gem' to 'gem' in code

Just replace 'require_gem' method with 'gem' and you're good to go.

MORE INFO:
Comments