Showing posts with label RubyonRails. Show all posts
Showing posts with label RubyonRails. Show all posts

Radio Button

<% form_for(@vote) do |f| %>
  <%= f.error_messages %>

  <% @survey_options.each do |survey_option| %>
    <table>
      <tr>
        <td>
             <%= f.radio_button 'survey_option_id', survey_option.id %>
             <%= f.label(:survey_option, survey_option.name) %>
       </td>
     </tr>
   </table>
  <% end %>

  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

hidden_field

<%= f.hidden_field :survey_title_id, :value => @survey_title.id %>

Namespace Tutorial

ruby script/generate scaffold SurveyOption name:string disable:boolean survey_title:references

rake db:migrate

ruby script/generate controller Admin::Survey_Options

map.namespace :admin do |admin|
admin.resources :survey_options
end

copy all codes in controller/survey_options_controller.rb to controller/admin/survey_options_controller.rb

change redirect_to(@survey_option) to redirect_to([:admin, @survey_option]) and redirect_to(survey_option_url) to redirect_to(admin_survey_options_url)

copy all files in views/survey_options to views/admin/survey_options

edit views/admin/survey_options/edit, index, new, show files.


http://railsforum.com/viewtopic.php?id=21742
http://www.akitaonrails.com/2007/12/12/rolling-with-rails-2-0-the-first-full-tutorial

Better than Bort - BaseApp

To use BaseApp read the tutorial below
  • http://github.com/ariejan/baseapp/tree/master
  • http://vladzloteanu.wordpress.com/2009/07/13/rails-startup-app-basecamp-user-authentication/

MySQL Socket

To find the correct path to mysql socket
  • mysql_config --socket

Column Properties

String - shot character
Text - long string
Integer - no decimal
Boolean - true or false
Float - number with decimal
Date - calendar date

Using Bort

Download and extract Bort - http://github.com/fudgestudios/bort/tree/master

Configure
  • config/settings.yml
  • config/database.yml
  • db/migrate/bort_migration.rb

Command
  • rake db:create
  • rake db:migrate

Recaptcha

http://deadprogrammersociety.blogspot.com/2008/02/recaptcha-on-rails.html

http://recaptcha.net

http://github.com/ambethia/recaptcha/tree/master

Model Validation

Validation Helpers
  • validates_acceptance_of
  • validates_associated
  • validates_confirmation_of
  • validates_exclusion_of
  • validates_format_of
  • validates_inclusion_of
  • validates_length_of
  • validates_numericality_of
  • validates_presence_of
  • validates_uniqueness_of
  • validates_each
Common Validation Options
  • :allow_nil
  • :allow_blank
  • :message
  • :on
http://guides.rails.info/activerecord_validations_callbacks.html

How to get the user_id

@comment.user = User.find(session[:user_id])

How to random select

@story = Story.find(:first, :order => 'RAND()')

Programming Best Practices

Don't Repeat Yourself
Stick to the Conventions
Optimize Later
Humans First
Test Driven Development
Refactoring

http://www.railsforum.com/viewtopic.php?id=1041

Active Record Methods and Options

ActiveRecord Methods => Used in Controller
ActiveRecord Options => User in Model

Routing _path and _url

_path is used in Views

_url is used in Controller

Basic Git Commands

git clone git@desiremesh.unfuddle.com:desiremesh/newsgroup.git
git commit -a -m 'comments here'
git push

Ruby on Rails Gems

http://docs.rubygems.org/
http://gems.github.com

Passenger for Ruby on Rails on Dreamhost

Modrails - Apache, Linux Webserver Installer
Passenger Wiki
Dreamhost Blog

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

Step by Step Guide in creating Project

  • Rails project_name
  • Delete public/index
  • Generate controller - Plural
  • Generate model - Singular
  • Setup database.yml
  • Rake db:create then migrate
  • Setup config/routes
  • Setup controller
  • Setup model
  • Setup view
Pattern for model, view, controller
  • index
  • show
  • new
  • create
  • edit
  • update
  • delete
Install and test plugins
  • paperclip - photo attachment
  • thinking_sphinx - full text search
  • will_paginate - pagination

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