Archive

Posts Tagged ‘ruby’

Rails Note #7: NIL in Fixtures

November 18th, 2008 rupert Comments off

If you want user_id to be ‘nil’, then omit it from your fixtures. I tried setting user_id: nil before, and it turned out to be ’0′ in the database.

# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
<% u = UserLogin.create( :password => 'foo') %>
<% u.password=('password') %>
 
user_no_profile:
 email: noprofile@yahoo.com
 admin: false
 user_id: nil
 salt: <%= u.salt %>
 salted_password: <%= u.salted_password %>

Picture 3.png

# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
<% u = UserLogin.create( :password => 'foo') %>
<% u.password=('password') %>
 
user_no_profile:
 email: noprofile@yahoo.com
 admin: false
 salt: <%= u.salt %>
 salted_password: <%= u.salted_password %>

Picture 2.png

Before pulling your hair out on what went wrong with your functional tests, check the test database if you have the correct values in your records. Remember ’0′ is different from ‘nil’.

Categories: rails, ruby Tags: ,

Rails Note #6: Pagination

November 16th, 2008 rupert Comments off

1. will_paginate docs
Main http://github.com/mislav/will_paginate/wikis
Reference http://mislav.uniqpath.com/static/will_paginate/doc/
Clone URL: git://github.com/mislav/will_paginate.git

2. Installation as a gem
http://github.com/mislav/will_paginate/wikis/installation
gem sources -a http://gems.github.com

Once installed, do script/server

3. Controller

class PoiAppController < ApplicationController 
  def poi_by_category
	mylimit = 100
    sql = "SELECT pa.* FROM poi_apps pa WHERE pa.id IN " +
          "(" +
			  "SELECT pc.poi_app_id " +
			  "FROM poi_categories pc " +
			  "WHERE pc.categ_node_id LIKE '" + params[:node_id] + "%%'" +
			  "LIMIT " + mylimit.to_s +
          ")"
    #@poi_apps = PoiApp.find_by_sql(sql)
    @poi_apps = PoiApp.paginate_by_sql [sql], :page => params[:page], :per_page => 10
 
  end
end

4. View

<h2>POIs</h2>
 
<div clas="page_info">
  <%= page_entries_info @poi_apps %>
</div>
 
<table>
  <tr>
    <td>POI_APP_ID</td>
    <td>CN_NAME</td>
  	<td>EN_NAME</td>
  </tr>
	<% for poi in @poi_apps %>
	<tr>
	  <td><%= poi.id %></td>
	  <td><%= poi.cn_name %></td>
		<td><%= poi.en_name %></td>
	</tr>
	<% end %>
</table>
 
<%= will_paginate @poi_apps %>

5. Checkout the styles http://mislav.uniqpath.com/static/will_paginate/

Categories: rails, ruby Tags: ,