Showing posts with label rails. Show all posts
Showing posts with label rails. Show all posts

Tuesday, April 28, 2009

some notes

1. do find => rescue

2. model operation more than 3 lines => define class method in model

3. def current_user

4. render_as_form

5. more than one relation in one table

6. composite primary key => hook

 

Wednesday, March 25, 2009

Rails on FreeBSD 7.1

Today I was first time trying to install rails on freebsd 7.1. Neither the ports version nor the gem file version, rails could not work, even only simply creating a new project. It returns message as following.

undefined method `camelize' for "app":String

I had try to reinstall ruby, rubygem, and try other version rails but a nonsense. And finally I forgot how did I discover it needs ruby-iconv ports in fact. It seems occurring at the time I was using rails 2.2.2, some keyword appeared in the error message.

One post on the mailing list [Ruby on Rails : Talk] show the same problem. If you also have the same problem, try it.

 

Saturday, July 12, 2008

restful 與 form_for

不可以在 /news/new 的 view 裡面寫 form_for @news ~"~

 

Tuesday, June 3, 2008

operate existed tables in rails

Just need specify table name and primary key in Model. All attribution in tables will appear automatically.
class ModelName < ActiveRecord::Base
self.table_name = "table_name"
self.primary_key = "primary_key"
end


 

Saturday, April 19, 2008

has_many() function note

if has_many :order
order.delete_all just set foreign key null
order.destroy_all clear all sub record
 
 

Friday, April 18, 2008

apache2 and mongrel cluster on debian

Enable apache2 proxy
$ sudo a2enmod proxy proxy_http proxy_balancer

Configure Proxy in a virtual host
/etc/apache2/site-enabled/default add
DocumentRoot "RailsApp/public"
    # even only RailsApp also work

ProxyPass / balancer://localhost/
ProxyPassReverse / balancer://localhost/
< proxy balancer://localhost/ >
    BalanceMember http://localhost:3000
    BalanceMember http://localhost:3001
    BalanceMember http://localhost:3002
< /proxy >


Then set mongrel cluster
$ sudo gem install mongrel_cluster
$ cd RailsApp
$ mongrel_rails cluster::configure -e development -p 3000 -N 3
    # write config/mongrel_cluster.yml

$ mongrel_rails cluster::start


Final, restart apache2 and it work.

 
 

Thursday, April 17, 2008

rss feed in rails

Note for the Way to create Atom and RSS 2.0 feed.

$ ./script/generate controller feed

Suppose that the model we want show in feed is Post

class FeedController < ApplicationController
    def rss
        @rss = @@post
    end

    def atom
        @atom = @@post
    end

  private
    @@post = Post.find :all,
        :order => "update_at", :limit => 10

end


Then write the view.

app/view/feed/rss.rxml

01 xml.instruct! :xml, :version=>"1.0"
02 xml.rss(:version=>"2.0"){
03     xml.channel{
04         xml.title("The site title")
05         xml.link(url_for("/"))
06         xml.description("The site description")
07         xml.language('en-us')
08
09         @rss.each do |rss|
10         xml.item {
11             xml.title rss.title
12             xml.link ""
13             xml.description rss.content
14             xml.pubDate rss.update_at
15             xml.guid ""
16             xml.author rss.declarer
17         }
18         end
19     }
20 }



app/view/feed/atom.rxml

01 xml.instruct! :xml, :version=>"1.0"
02 xml.feed "xmlns" => "http://www.w3.org/2005/Atom" do
03     xml.title("The site title")
04     xml.link(url_for "/")
05     @atom.each do |atom|
06         xml.entry {
07             xml.title(atom.title)
08             xml.summary(atom.content)
09         }
10     end
11 end


app/view/layout/application.rhtml add

<%= auto_discovery_link_tag :rss, { :controller => '/rss', :action => 'rss' }, { :title => "RSS 2.0" } %>
<%= auto_discovery_link_tag :rss, { :controller => '/rss', :action => 'atom' }, { :title => "Atom" } %>


 
 

Tuesday, April 15, 2008

rails helper block

Write for memo.
To set a helper in the form

<% helper_name do %>
    <div>blah</div>
<% end %>
.

The definition is

def helper_name(&block)
    content = capture(&block)
    concat("what we want write before block".block.binding)
    concat(content,block.binding)
    concat("what we want write after block",block.binding)
end


By the way, partial is the way do the same thing once. if the outer html is used frequently, the way to define a helper as above will be better.
 
 

Sunday, April 13, 2008

spakit

The rails plugin spakit means Single Page Application KIT. It can transform rails app into single page app but not modify app structure in large scale by loading content of other controller into primary controller.

The installation step is from handlino.com
$ sudo gem install spakit
$ cd railsapp/vendor/plugins/
$ gem unpack spakit
$ mv spakit-version spakit

now installation finished.

Before using it, spakit need a layout railsapp/app/view/layout/spakit.rhtml<%= flash[:notice] %>
<%= yield %>

And the layout application.rhtml should contain
<%= javascript_include_tag :defaults %> to include default js file.

Now we can use it happy. Add <div id="content"></div> into the view of primary controller, and set a spakit link: <%= spakit_link_to 'new person', :url => new_person_path %>, the content of the url will appear in the div#content.

Also two other helpers contain in spakit, spakit_form_for and spakit_form_tag

link1: spakit at rubyforge
link2: spakit at github

annotate models

The plugin used for adding column annotate into model file is introduced in Ralls Bible. Write for note. the newer introduction at http://pragdave.pragprog.com/pragdave/2006/02/annotate_models.html. Just two step to use it.
1.script/plugin install http://repo.pragprog.com/svn/Public/plugins/annotate_models
2. rake annotate_models
And all model now with column annotate at file head.