I’ve decided to create a different blog to log all of the interesting tech-related problems I encounter everyday. More details at the first post.
Suggestions (esp. a better tagline) and contributions are welcome.
Form is emptiness, emptiness is form.
I’ve decided to create a different blog to log all of the interesting tech-related problems I encounter everyday. More details at the first post.
Suggestions (esp. a better tagline) and contributions are welcome.
Member routes allow one to create additional routes to existing resources. For example, if you want to add an approve path to your purchases resource, you could do this in your config/routes.rb:
map.resources :purchases, :member => { :approve => :put }
My problem was that the link I made for approving the purchase
<%= link_to "Approve Purchase", approve_purchase_path(@purchase), :confirm => "Proceed with approval?" %>
resulted in the following error:
Unknown action
No action responded to 1. Actions: approve, create, destroy, edit, index, new, show, and update
Rails confused the link with a normal get operation.
Explicitly declare the HTTP method for the action.
<%= link_to "Approve Purchase", approve_purchase_path(@purchase), :confirm => "Proceed with approval?", :method => :put %>
Rails doesn’t automatically convert newline (“\n“) characters in strings to line breaks (“<br>“) with h.
<%= h @user.address %>
Manually replacing the \n with <br> without using h will make your site vulnerable to XSS attacks. The proper approach would be to convert the line breaks after h. Note that Rails already has a method, simple_format, that converts line breaks.
<%= simple_format(h @user.address) %>
When developing for Ruby on Rails, the development server (the one used by script/server) feels slow and unresponsive.
RoR uses WEBrick as the default server. Based on my experience on a Ubuntu virtual machine with 1GB of RAM, this server doesn’t respond as fast as the web application servers in other web languages (Apache, Tomcat, IIS).
Install Mongrel. In Ubuntu 9.04, you can install this using:
sudo gem install mongrel
In case you receive build errors, you might have to install ruby-dev to deal with the dependency issues.
sudo apt-get install ruby-dev
Once installed, Rails will automatically use Mongrel as the application server over WEBrick.
UPDATE: Mongrel’s pretty much dead by now. Use thin instead.
Just add gem 'thin' to your Gemfile and run bundle install.
The following line of code for calculating the total order cost returns “#”.
@purchase_items.reduce() {|cost, item| cost += item.qty_ordered * item.unit_price }
The default memo item for a reduce operation in Ruby is the first item on the list (because of this, it starts the reduce on the second item).
You can set the initial memo item by passing it to the reduce function.
@purchase_items.reduce(0) {|cost, item| cost += item.qty_ordered * item.unit_price }