Ruby on Rails : rateatrack app notes to self

add in item to an array/object example:

@artists << track.artist

order object contents example:

def index
@tracks = Track.all(:order => ‘artist‘)

simple rating average math:

rating = params[‘rating’].to_i
@track.average = ((@track.average * @track.numberofvotes) + newlysubmittedrating)/(@track.numberofvotes + 1)
@track.numberofvotes = @track.numberofvotes + 1

simple db update conditional redirect with reporting:

if @track.update_attributes(params[:track])
format.html { redirect_to(“/”, :notice => ‘Track updated.’) }

similar db object one liners:

  • @object.save
  • @object.destroy
  • @track = Track.find(params[:id])
  • @object = Track.new
  • @record = Track.find_by_sql “SELECT * …”

simple char substitution:

somevariablename = variablecontainingcharstoswap.gsub(“_”, ” “)

method simply needs to render a view other than that matching its name:

render :viewname

.each loop fills text variable, then renders it:

@record.each do |track|
$str << “#EXTINFO:#{track.length},#{track.title}<br />#{track.url}<br />”
end
render :text => $str

notice: to substitute variables into such a statement, #{variablename}

simple and example model with validation and some messaging:

class Track < ActiveRecord::Base
validates :url,  :presence => true
validates :title, :presence => true, :length => { :minimum => 1 }
validates :artist, :presence => true
validates :length, :presence => true
validates_format_of :url, :title, :artist, :length, :with =>/^[a-zA-Z \\ \/. : 0-9]+$/x, :message=>”alphanumeric characters, colons, periods, forward/backward slashes only please”
end

routes.rb

Rateatrack::Application.routes.draw do

match “tracks/generate/” => “tracks#generate” // custom :action route
resources :tracks

get “home/index”

root :to => “tracks#index” // sidestep app index, route to desired controller
end

heroku only uses Postgres, used MySQL locally, fun experiment to RELY on persistence:

database.yml

development:
adapter: mysql2
encoding: utf8
reconnect: false
database: rateatrack_development
pool: 5
username: user_name
password: password_removed
socket: /tmp/mysql.sock

production:
encoding: unicode
adapter: postgresql
database: rateatrack_production

simple schema.rb

ActiveRecord::Schema.define(:version => 0) do

create_table “tracks”, :force => true do |t|
t.string  “url”,     :limit => 64
t.string  “title”,   :limit => 64
t.string  “artist”,  :limit => 64
t.integer “length”
t.decimal “average”, :default => 0.0, :precision => 8, :scale => 2
t.integer “votes”,   :default => 0
end

end

RoR version of include, partial, snippet, and so on…

<%= render ‘form’ %>

this will pull from the same folder as the calling resource, file name _form.erb

form example with error checking:

<% @track %>
<%= form_for(@track) do |track_form| %>
<% if @track.errors.any? %>
<%= pluralize(@track.errors.count, “error”) %> prohibited this post from being saved:
<% @track.errors.full_messages.each do |msg| %>
<%= msg %>
<% end %>
</ul>
</div>
<% end %>
<table><tr><th><%= track_form.label :url %></th></tr>
<tr><td><%= track_form.text_field :url %></td></tr></table>
<%= track_form.submit %>
<% end %>

Gemfile

gem ‘rails’, ‘3.0.4’
gem ‘mysql2’

One Response to “Ruby on Rails : rateatrack app notes to self”

  1. Tweets that mention alexyz» Blog Archive » Ruby on Rails : rateatrack app notes to self -- Topsy.com Says:

    […] This post was mentioned on Twitter by Ruby on Rails UK, RubyOnRails Ireland. RubyOnRails Ireland said: alexyz» Blog Archive » Ruby on Rails : rateatrack app notes to self http://bit.ly/hxG5x3 […]

Leave a Reply

You must be logged in to post a comment.