Showing posts with label Tutorial. Show all posts
Showing posts with label Tutorial. Show all posts

Desiremesh - Setup Newsgroup

git clone git@desiremesh.unfuddle.com:desiremesh/newsgroup.git

Install Gem
sudo gem install rmagick hpricot htmlentities RedCloth rake haml aws-s3

Setup database.yml
rake db:create
rake db:migrate
ruby script/server

Active Record Random

Installation:
ruby script/plugin install git://github.com/norman/active_record_random.git

Controllers:
@countries = Country.find(:all, :order => :random, :limit => 10)

Controller using will_paginate:
@countries = Country.paginate :page => params[:page], :per_page => 20, :order => :random

Views:
<% for country in @countries %>
<%=h country.name %>
<% end %>

<%= will_paginate @countries %>

http://github.com/norman/active_record_random/tree/master

Routing _path and _url

_path is used in Views

_url is used in Controller

Thinking Sphinx Tutorial with Delta Indexing

ruby script/plugin install git://github.com/freelancing-god/thinking-sphinx.git
ruby script/plugin install git://github.com/mislav/will_paginate.git # to use will_paginate

In Model
define_index do
indexes searchable_fields, :sortable => true # to sort the fields
set_property :delta => true # no need to run rake ts:index everytime there is a new data entry, but its really recommend to run rake ts:index everyhour or everyday
end

In Controller
def search
@string = Model.search params[:search], :page => params[:page], :per_page => no_of_page, :order => :sortable_fields
end

In View
<% for person in @Person %>
<%= person.name -%> <%= link_to "show", {:action => 'show', :id => person.id} -%>
<% end %>

<%= will_paginate @person %> # to activate pagination

Create boolean delta in model
ruby script/generate migration add_boolean_data

def self.up
add_column :model, :delta, :boolean, :default => false
end

rake db:migrate

Terminal Commands # run this command to activate delta indexing
rake ts:stop # ts or thinking_sphinx
rake ts:index
rake ts:start

Railscasts
Resources

link_to syntax

<%= link_to display_text, link, { option } %>

The three options specific to link_to (:confirm, :popup, and :method)
link_to "Visit Other Site", "http://www.rubyonrails.org/", :confirm => "Are you sure?"
# => Visit Other Site

link_to "Help", { :action => "help" }, :popup => true
# => Help

link_to "View Image", @image, :popup => ['new_window_name', 'height=300,width=600']
# => View Image

link_to "Delete Image", @image, :confirm => "Are you sure?", :method => :delete
# => Delete Image

Others:
  • link_to_if
  • link_to_unless
  • link_to_unless_current
  • mail_to
  • url_for


http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#M001565

Active Record Tutorial

Types of Associations
  • belongs_to
  • has_one
  • has_many
  • has_many :through
  • has_one :through
  • has_and_belongs_to_many
http://guides.rails.info/association_basics.html

Paperclip Tutorial

script/plugin install git://github.com/thoughtbot/paperclip.git
script/generate paperclip product photo
rake db:migrate

# models/product.rb
has_attached_file :photo, :styles => { :small => "150x150>" },
:url => "/assets/products/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/products/:id/:style/:basename.:extension"

validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']


# views
<%= image_tag @product.photo.url(:small) %>


<% form_for @product, :html => { :multipart => true } do |f| %>
...
<%= f.file_field :photo %>
...
<% end %>