The default behavior of blogger is to insert <by /> for all line breaks when publishing posts. It does this even if you chose to edit in html. If this is annoying you and causing your blog to look a bit messed up, you can stop it by changing the 'Settings->Formatting->Convert line breaks" option in the blog panel.
Monday, January 14, 2008
Sunday, January 13, 2008
Ruby single/double quotes
Both single and double quotes can be used for string. The difference is in the way they are interpreted. Double quotes are 'interpreted more'. That is to say the parser will look at them and replace escape characters with newlines, insert variables etc.
Single quotes
Single quotes only support the escape sequences
- \' - single quote
- \ - single backslash
If you have a phrase were this would create confusion, making the code difficult to read you choose a different delimiter using %q :
puts %q!c:\napolean's documents\tomorrow's bus schedule.txt!
puts %q/c:\napolean's documents\tomorrow's bus schedule.txt/
puts %q^c:\napolean's documents\tomorrow's bus schedule.txt^
puts %q(c:\napolean's documents\tomorrow's bus schedule.txt)
puts %q{c:\napolean's documents\tomorrow's bus schedule.txt}
See the ruby programming wiki.
Double quotes
Double quotes support many more escape sequences and you can use these to substitute variables into strings:
puts "Enter name"
name = gets.chomp
puts "Your name is #{name}"
For more control sequences see the strings page of the ruby programming wiki. You can also do a similar trick to the above using %Q.
Saturday, January 12, 2008
Rails Routing
map.connect ':controller/:action/:id'What will this do? The argument after the map.connect tells the server a string to match and what to call the bits it matches. Here when the server receives "http://www.mysite.com/user/show/12" it first strips off the web address and so is left with "user/show/12". It will then look at this rule and match the form and asign the string 'user' to :controller, 'new' to :action and '12' to :id. It will then call the 'user' controller with the 'new' action and id = 12. This is a pretty basic rule. How about this one:
map.connect 'date/:year/:month/:day', :controller => 'blog', :action => 'display_date'this would match "www.mysite.com/date/2008/1/1" to the 'display_date' method of the 'blog' controller with :year => 2008, :month => 1, :day => 1. To specify that some of the match pattern is optional (to match www.mysite.com/date/2008) say, you can add
:month =>nil, :day=>nilto the rule. To stop too much matching (eg to stop the above matching www.mysite.com/date/latest ) you can add
:requirements => {:year => /\d{4}/, :month => /\d{1,2}/, :day => /\d{1,2}/}
This allows you to use the full power of regular expressions when matching.
To add a default eg
map.connect 'posts/:category', :controller => 'blog', :action => 'posts', :category => :allAt first glance there doesn't seem much point to doing this as :category could default to :all in the controller. However the payoff comes when 'going backwards' using link_to and url_for. If you didn't have the default set you would have to write
link_to 'Posts', :controller => 'blog', :action => 'posts', :category => :alland also "all" would be displayed at the end of the url in the browser. In the link_to syntax the first argument is the name to call the link (ie the link as shown to the user) and the rest of the arguments the stuff necessary to match to a url by looking in the routes.rb file. For more explanation of the above (where I copied the examples from :p ) see the rubyonrails manual Remember that the server starts at the top and works down so that rules at the top have higher precedence. See here for a deeper look at what's going on. Note "map.root" is equivalent to "map.connect ' ', :controller => 'index', :action => 'index' " (I think).
Using rails for a static site
I wanted to add an index and about page to my site but avoid repeating the overall page design stored in app/views/layouts/application.html.erb. I found the basic idea on Robert Evans' site. The application.html.erb file is what the rails routing machine falls back on as a default template if it doesn't find one of the same name as the controller.
- Create a new controller to manage the pages. At the control line (positioned within your application folder) enter:
script/generate controller static index about
The script/generate contoller bit tells rails to create the controller file (app/controllers/static_controller.rb) and the associated views folder (app/views/static/) . The next argument (static) is the name of the controller to be created. The final arguments (index, about) are optional and tell rails to create methods of those names in the controller file and the appropriate files in the views folder (eg. app/views/static/index.html.erb). - Edit the view files to contain the required content
- You can customise parts of application.erb.html for example create a custom title by changing the title line in the header to
<title>YourSiteName <%= @page_title if @page_title %></title>
and modifying the methods in the controller file todef index @page_title = "Home" end - Add the following to the "config/routes.rb" file
map.root :controller => 'static', :action => 'index'
- Refer to the pages using
link_to :controller => 'static', :action => 'index' link_to :controller => 'static, :action => 'about'