Tech in T: depth + breadth‎ > ‎Tools - Business‎ > ‎Ruby‎ > ‎cs169‎ > ‎


# instead of doing in one step with create! do it in two steps with new, if (save)

in control you can print params by the following statement: params

Rails not updating CSS
$ bundle exec rake assets:precompile

Heroku logs
heroku logs -n 1500 
OR, to tail the logs live 
heroku logs -t

To use a class vaiable @@ you need to define a getter for it
class Movie < ActiveRecord::Base
  @@ALL_RATINGS = ['G','PG','PG-13','R','NC-17']
  def self.ALL_RATINGS

Buildup a query

@users = User.limit(10)
@users = @users.where('id', params[:id]) if params[:id]
@users = @users.where('email', params[:email]) if params[:email]

It isn't the prettiest, but it will work. However, I recommend keeping it to a single method call and defining it in the model.

# In the model

def self.by_id_and_email(id, email)
  users = limit(10)
  users = users.where('id', id)       if id.present?
  users = users.where('email', email) if email.present?


# In the controller / out of the model

User.by_id_and_email(params[:id], params[:email])

Remain Restful!

 To be RESTful, we want to preserve the property that a URI that results in a sorted/filtered view always contains the corresponding sorting/filtering parameters. Therefore, if you find that the incoming URI is lacking the right params[] and you’re forced to fill them in from the session[], the RESTful thing to do is to redirect_to the new URI containing the appropriate parameters. There is an important corner case to keep in mind here, though: if the previous action had placed a message in the flash[] to display after a redirect to the movies page, your additional redirect will delete that message and it will never appear, since the flash[] only survives across a single redirect. To fix this, use flash.keep right before your additional redirect.

Multiple Page form to edit

If you get
 could not connect to server: Connection refused        Is the server running on host "" and accepting        TCP/IP connections on port 5432?
$ vim config/application.rb      right before module      config.assets.initialize_on_precompile = false

For associations:
in migration:
t.belongs_to :customer
in model none
in controller...

instead of create! use new-save and for update_attributes! use update_attributes and check for validations and render relevant action. Display errors there
= form_tag store_path(@store), :method => :put do
  - if @store.errors.any?
      %b Errors
        -@store.errors.full_messages.each do |e|
            = e
  = label :store, :name, 'Name'
  = text_field :store, 'name'

  = label :store, :address, 'Address'
  = text_field :store, 'address'

  = label :store, :description, 'Description'
  = text_area :store, 'description'

  = submit_tag 'Save Changes'

heroku run rake db:schema:load  # reload the whole schema

Subpages (1): HW2