Showing posts with label rubyonrails. Show all posts
Showing posts with label rubyonrails. Show all posts

Sunday, April 13, 2008

Rails attr_accessor is an instance variable

It took me some time to truly understand rails attr_accessor macro method though I've already used it a lot before.

Attr_accessor, along with attr_reader and attr_writer, they are in fact ruby things not rails. Attr_accessor defines instance variables and builds the get/set methods for each instance variable.

To know the difference between ActiveRecord's attribute (for table field) and the attr_accessor, the best way is to give an example, let's see:

class User > ActiveRecord::Base
attr_accessor :password #password is not a user's table field
validates_presence_of :login
end

##########################

[/home/jack/test_app]$ script/console
Loading development environment (Rails 2.0.2)
>> user = User.new(:login => "jimmy", :password => "railsrocks")
=> #
### password is not a field

>> user.instance_variables
=> ["@attributes_cache", "@attributes", "@password", "@new_record"]
>> user.instance_variable_get :@login
=> nil
>> user.instance_variable_get :@password
=> "railsrocks"
>> user.has_attribute? "login"
=> true
>> user.has_attribute? "password"
=> false
### login is an attribute, while password is a instance variable

>> user.login
=> "jimmy"
>> user.password
=> "railsrocks"
>> user.send 'login'
=> "jimmy"
>> user.send 'password'
=> "railsrocks"
### both getter/setter are working
### in some situation, for example, the field name itself is a variable
### you can use: user.send method_name

>> user[:login]
=> "jimmy"
>> user.attributes
=> {"login"=>"jimmy"}
>> user[:password]
=> nil
### oops, I can not get password by this way

>> user.attributes={:login => "grissom", :password => "rubyrocks"}
=> {:password=>"rubyrocks", :login=>"grissom"}
>> user.password
=> "rubyrocks"
### well, to assign value is working

Thursday, April 10, 2008

Design Free Administration Back Office For Rails Application

There are some common concerns to build a back office for a rails project. Not like the front pages, which are usually designed by professional web designers, back office pages often need to be scratched by programmers. It often takes me a lot of time to do the layout, HTML structures, header and footer, div content boxes, list table styles and form field styles. Though I am familiar with CSS, I still troubled a lot with the overall look and feel because of my lack of the web design gene. So I am writing here to conclude a way to be design fee....

1. Install my plugin uni_admin for admin layout, table styles:

http://code.google.com/p/ruby-on-rails-plugin-uni-admin/

2. Install uni-form and my uni-form-patch for form styles

http://code.google.com/p/rails-uni-form-patch/

3. Install plugin restful authentication for basic authentication

http://xiaoboonrails.blogspot.com/2008/02/install-and-config-rails-plugin.html

4. If you have installed comatose, it is easy to integrate comatose_admin with uni_admin layout
rake comatose:admin:customize
update related content of layouts/uni_admin_layout.html.erb to layouts/comatose_admin.rhtml

Rails Plugin Uni_Form_Patch for Plugin Uni-Form

UniFormPatch
============
A small patch plugin for Marcus Irven's uni-form plugin.

Code home: http://code.google.com/p/rails-uni-form-patch/

Fix the following features:
  • Make the form show properly
  • Show field focus and error background images
  • Show error and success form messages properly

Installation
=========
If you havn't installed uni-form, install it at first:
script/plugin install http://uni-form.rubyforge.org/svn/trunk/plugins/uni-form/

Follow the installation notes in Marcus's uni-form README to finish installation

Install uni_form_patch:
script/plugin install http://rails-uni-form-patch.googlecode.com/svn/trunk/uni_form_patch

The installation will automatically copy/update/delete files:
Copy public/images/uni-form/*.png
Update public/stylesheets/uni-form.css
Delete public/stylesheets/uni-form-generic.css

Example
=======
For the usage of uni-form, read Marcus's uni-form README.

Show uni-form style messages:
<% uni_form_for :user do |form| %>

<%= uniform_success_message flash[:notice] if flash[:notice] %>
<%= uniform_error_messages [@user] %>
<%= uniform_error_messages [@user, @address] %>

<% form.fieldset :type => "block", :legend => "cool stuff" do %>
<%= form.text_field :first_name, :required => true, :label => "Your first name" %>
<%= form.text_field :last_name %>
<% end %>
<%= form.submit "save" %>
<% end %>

Ruby On Rails Plugin Uni Admin For Admin Layout

Today I code a new simple plugin Uni_Admin. Uni Admin is a simple administration layout support for ruby on rails web application. Now it includes:
  • an admin base controller and dashboard action
  • a simple admin layout
  • a simple admin CSS file
  • an integrated admin menu
  • a record list table style
Demo:


More details please see the code page:

http://code.google.com/p/ruby-on-rails-plugin-uni-admin/

Wednesday, April 9, 2008

KikOut.com Beta Version 0.2 Released

My ruby on rails project KikOut.com released beta version 0.2.

It is a search engine to help you facilitate your web searching, for example, using youtube:keyword to search youtube directly. You will dig more fun after using it. More information about Kikout please check out the Kikout why&howto page.

Welcome make your own search definitions or share your experiences/submit your comments.

Monday, March 17, 2008

Rails send emails with actionmailer and gmail

The first step is to configure your rails application to work with gmail, I just copied the 2 simple steps from the Preston Lee's article:

1. Save this code as lib/smtp_tls.rb within your rails app;
2. Add this code to config/environment.rb

The second step is to learn to use actionmailer, just follow the document "How to send emails with action mailer" to do it then you will understand how it works pretty fast.

1. ruby script/generate mailer Notifier signup order_created
2. add code to your app/models/notifier.rb, such as

def signup( user )
# Email header info MUST be added here
recipients user.email
from "accounts@mywebsite.com"
subject ¡°Thank you for registering with our website¡±
# Email body substitutions go here
body :user=> user
end

3. add code to /app/views/notifier/signup.erb
Dear <%= @user.first_name %> <%= @user.last_name %>,
Thanks for signing up with My Website!

4. use the mailer in where you want.
Notifier.deliver_signup(user)

Tuesday, February 26, 2008

The way to have global constants in rails application

I don't know if there is a better or formal way to keep global constants for rails application. If I want to the global constants to be used only by controllers and views, the best way is to declared them in application helper. If active record is also to use them, I have to put the definitions in environment.rb. Like the following:

# Global constant for this application
# I define an array here
SOME_GLOBAL_INSTANT = [100, 300, 500, 800, 1000]

I googled by "rails global constants" but get no better solutions. Anyone knows this please tell me.

Another way to load config into controllers is to use YAML file, check out this blog post:

http://snippets.dzone.com/posts/show/287

3 easy steps to do Rails 2.0 pagination

Forget about the kind of awkward pagination of rails 1.x, now you need 3 easy steps to do Rails 2.0 pagination.

1. Install the pagination plugin:

ruby script/plugin install svn://errtheblog.com/svn/plugins/will_paginate

2. Type the following in your controller:

@products = Product.paginate(:page => params[:page], :per_page => 25)

3. Type the following in your view:

<%= will_paginate @products %>

The better thing comparing to rails 1.x pagination is that it is now easier to understand and remember. Isn't it more conventional?

Monday, February 25, 2008

Basic flow to use rails file_column plugin

1. Create your table with a string field to store file name. For example:

create_table :products do |t|
t.string "name"
t.string "model"
t.string "image"
t.timestamps
end

2. Install file_column plugin:

script/plugin install http://opensvn.csie.org/rails_file_column/plugins/file_column/trunk

3. Install ImageMagick to your system and plugin RMagick

Visit http://rmagick.rubyforge.org/ for more detail

4. Put file_column description for model field into your model code. For example here, add to product.rb:

file_column :image, :magick => {
:versions => { "thumb" => "60x60", "medium" => "320x240"}
}

5. Generate controller and views for your model. I use rails 2.0 scaffold generator here:

script/generate scaffold product --skip-migration
add layout and authentication before_filter to product controller if needed
add _form.html.erb with form fields for new.html.erb and edit.html.erb to use
edit index.html.erb to show the list of products, such as <%= f.text_field :name %>

6. Show and upload image of product, edit in the _form.html.erb:

remove <%= f.text_field :image %>
add:
<% if !@product.id.nil? # in the case of edit %>
<%= image_tag url_for_file_column("product", "image", "medium") %>
<% end %>
<%= file_column_field "product", "image" %>

7. Add multipart to form tag in the edit and new files.

<% form_for(@product, :html => {:multipart => true}) do |f| %>

8. To show image in the list:

<% @product = product %>
<%= image_tag url_for_file_column("product", "image", "thumb") %>

Sunday, February 24, 2008

Tips to use scaffold generator of rails 2.0

There are big differences for the scaffold between rails 2.0 and rails rails 2.0 scaffold", like me, you can find a lot of good blogs or articles to learn about the new ways to use rails 2.0 scaffold. Anyhow, I would like to add some basic tips here, which I used a lot.

Number one tip is to use the help from rails itself:
script/generate scaffold -h

In rails 1.x, I created model and table in db firstly then do the scaffold, now the generator can do the model and table in the same time. The way to keep your old habit is to use:
> script/generate scaffold category(or other model name) --skip-migration

In the views, I need add the _form.html.erb by myself.

The generator will also put a map.resources in your routes.rb. If I want to add new actions such as batch_new and batch_create into this categories_controller. Config the routes.rb:
map.resources :categories,
:collection => {:batch_new => :get, :batch_create => :post}

Wednesday, February 20, 2008

Rails 2.0 tip: Create a new application with MySQL support

In rails < 2.0, when I create a rails new application by the rails command, the configuration of database.yml supports MySQL by default. While in rails 2.0, it will generate default configuration database.yml for Sqlite3. To make it default as MySQL, run with -d mysql.
rails -d mysql your_application_name

Thursday, February 14, 2008

Install and config rails plugin restful_authentication

Ruby on rails plugin Restful_authentication is a basic plugin useful for most of rails applications. It is very easy to intall and config it:
script/plugin source http://svn.techno-weenie.net/projects/plugins
script/plugin install restful_authentication
script/generate authenticated user sessions
rake db:migrate

Check the code in your routes.rb which is automatically added by the generation
map.resources :users
map.resource :session

Add more route mapping if your want
map.signup '/signup', :controller => 'users', :action => 'new'
map.login '/login', :controller => 'sessions', :action => 'new'
map.logout '/logout', :controller => 'sessions', :action => 'destroy'

Visit http://localhost:3000/users/new or http://localhost:3000/signup

Then to make it work with Comatose, add comatose config to evironment.rb of your app:
Comatose.configure do |config|
# Includes AuthenticationSystem in the ComatoseAdminController
config.admin_includes << :authenticated_system

# Calls :login_required as a before_filter
config.admin_authorization = :login_required
end

Thursday, February 7, 2008

Some problems of the installation of Rails Comatose Plugin

Comatose is a very handy content management ruby on rails plugin. Here I am listing some problems that I encountered when I get Comatose running correctly. Most of them can be easily fixed by googling. It is just a quick summary just in case you get the same kinds of problems or errors as I did.

To install it:
script/plugin install http://comatose-plugin.googlecode.com/svn/trunk/comatose
script/generate comatose_migration
rake db:migrate
# add to your routes.rb:
map.comatose_admin
map.comatose_root 'pages'
script/server
# visit content admin http://localhost:3000/comatose_admin
# after add a page of name My Page
# visit it at http://localhost:3000/pages/my-page

1. Do rake db migration, get a Mysql::Error: BLOB/TEXT column 'full_path' can't have a default value. To fix it, remove ":default => ''" from the definition of column 'full_path', in the migration file db/migrate/xxx_add_comatose_support.rb

2. To start your rails app, get an error `method_missing': undefined method `comatose_admin' for #. I got this routing error after I use Active Merchant plugin, can not figure out why. Add this to environment.rb to make Comatose plugin be loaded firstly:
config.plugins = [ :comatose, :all ]

3. Install necessary plugins
./script/plugin install acts_as_tree
./script/plugin install acts_as_list

4. To customize comatose admin view
rake comatose:admin:customize

This task will copy all of admin view rthml files to app/view/comatose_admin directory of your rails app. There is a new layout file comatose_admin.rhtml. Modify this layout file to customize your admin view.