Showing posts with label Ruby on Rails. Show all posts
Showing posts with label Ruby on Rails. Show all posts

Monday, December 21, 2009

drop down list/collection selection

I have a simple application just like blog tutorial in the book 'Agile Web Development with Rails', it has two models:
class Log < ActiveRecord::Base
belongs_to :product
end

class Product < ActiveRecord::Base
has_many :logs
end
Product has a part_number_id column which Log reference to. Log was created using scaffold while Product was created using script/generate model.

on the new.html.erb and edit.html.erb files for logs, I need to show all available products in a drop down list so user can choose the product and edit log for it. collection_selection is the one to do this.
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})

in new.html.erb, it's easier,
here:
object: is the log model
method: is the log model's attribute: part_number_id
collection: log's part_number_id is choose from Product model
value_method: the value displayed in select & option tag is Product's id
text_method: the text valude displayed in select & option tag is Product's part number
options{:include_blank => 'Please Select'}: when no option is available, display 'Please select' in the select & option tag.

in edit.html.erb, not only do i need to show all available products in dropdown list, also I need to select the part number of that specified log:
in the options, we added one more, :selected => @selected_log, the variable @selected_log was defined in controller/logs_controller.rb
# GET /logs/1/edit
  def edit
    @log = Log.find(params[:id])
    @selected_log = Product.find(@log.part_number_id).id
  end
it tells that the selected option is the one defined ass @selected_log.

Friday, December 18, 2009

checkbox

the model Log has a attribute :status which can be either true(valid) or false (invalid).

the view(edit.html.erb) has a form to show this model, want to use a checkbox to allow user to invalidate the log:
to populate the log's status to the check box:
if log's status is valid, the check box is not checked;
if log's status is invalid, the check box is checked;

to populate the check box's value back to log's status:
if user check the check box, the log's status will be set to false(invalid)
once the check box is checked, user is not allowed to uncheck it.

To Populate from Model to View
The tricky thing here is, the status of the check box is opposite to the log's status, so tried the following ways:
<%= f.label 'Invalide?' %><%= f.check_box !(:status)%>
or 
<%= f.label 'Invalide?' %><%= f.check_box (:status ? 0:1)%>
those wont' work, it complained either 'undefined method 'false' or undefined 0.

read the API again and googled around (kudos to rob-twf's reply at ruby on rails forum), here is the solution:
<%= f.label 'Invalide?' %><%= f.check_box :status, {:checked => !f.object.status} %>

follow the API:
check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0") 
object_name: here is our Log model,
method: here is our :status attribute, it has nothing to do with the checkbox's status(checked or not), that's what confused me in the first place
options: html options, here is the place to specify the checkbox's status, checked or not
checked_value & unchecked_value: can safely ignore them in this case.

so in order to set the checkbox checked, we defined the condition in options as:
{:checked => :status ? 0:1}
if log's status is true, uncheck the checkbox, otherwise, check it.

To Populate from View to Model
after the modification from above, we can populate the log.status value correctly to the view(edit.html.erb). then we checked the checkbox to invalidate the log and click Update button, it shows 'Log was successfully updated', however if we go to the database we can see the status is still valid(1). what is wrong?

go to the script/server screen, we found the following statements for the Update click:
Processing LogsController#update (for 127.0.0.1 at 2009-12-18 13:28:43) [PUT]
  Parameters: {"commit"=>"Update", "authenticity_token"=>"dQ3IIa6mI0IrJORrayVQjQa6RHn95AqfVvDahBglGMc=", 
"log"=>{"serial_number"=>"L-000099", "log"=>"Two zipper teeth are missing", "part_number_id"=>"4", 
"status"=>"1", "user"=>"Tom"}, "id"=>"3"}
  Log Columns (1.2ms)   SHOW FIELDS FROM `logs`
  Log Load (0.1ms)   SELECT * FROM `logs` WHERE (`logs`.`id` = 3) 
  SQL (0.1ms)   BEGIN
  SQL (0.1ms)   COMMIT
Redirected to http://localhost:3000/logs/3
Completed in 16ms (DB: 2) | 302 Found [http://localhost/logs/3]
  SQL (0.1ms)   SET NAMES 'utf8'
  SQL (0.1ms)   SET SQL_AUTO_IS_NULL=0
see there is no SQL statements between the BEGIN and COMMIT, the reason is nothing is changed for that log record, so no update to it.

How could that be possible? I just checked the check box to invalidate the log's status, the 'status' should be set to 0. well, go back to read the API further:
check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0") 
By default, if the the check box is checked, it's set to 1, otherwise, set to 0. that's the opposite to what I need here. so go back to edit.html.erb and update the code again:
<%= f.label 'Invalide?' %>
<%= f.check_box :status, {:checked => :status ? 0:1}, checked_value = "0", unchecked_value = "1" %>
now it's happy. without anymore change to the code, it can update the Log correctly.

Wednesday, October 28, 2009

ruby on rails installation on ubuntu 9.04

for god's sake, i have this serious problem with ruby on rails installation on ubuntu 9.04, i tried so many times with all possible ways to install and without exception it always failed! tried to loggin to their IRC to ask for help and got blamed by some arrogarant people saying that i was not good at following instruction! damn maybe i have problem with following instruction, but if RoR is stupid enough that only by following instruction step by step, why bother to use it anyway? however that's another story, let me focus on installing it first, all in all i still wanna try ruby on rails.

this time with help from others and the instruction from ruby on rails community:

1. install ruby:
sudo apt-get install ruby-full build-essential

2. install rubygems
sudo apt-get install rubygems1.8
sudo gem install rubygems-update

3. install rails:
sudo apt-get install rails 

4. install MySQL:
sudo apt-get install mysql-server mysql-client
sudo apt-get install libmysql-ruby libmysqlclient-dev
sudo gem install mysql

5. install apache:
sudo gem install passenger
sudo apt-get install apache2-threaded-dev libapr1-dev libaprutil1-dev
sudo /var/lib/gems/1.8/bin/passenger-install-apache2-module
then add the following to the apache configuration file:
LoadModule passenger_module /var/lib/gems/1.8/gems/passenger-2.2.5/ext/apache2/mod_passenger.so
PassengerRoot /var/lib/gems/1.8/gems/passenger-2.2.5
PassengerRuby /usr/bin/ruby1.8
 
6. setup virtual host: 
sudo emacs /etc/apache2/apache2.conf
add the following:
NameVirtualHost *:80
create file for ror.myhost.com:
susdo emacs /etc/apache2/sites-available/ror.myhost.com
and add the following:

ServerName ror.myhost.com
DocumentRoot /home/dan/Public/mynewapp/public
sudo emacs /etc/hosts
and add:
127.0.0.1  ror.myhost.com
then restart apache2:
sudo a2enmod rewrite
sudo a2ensite ror.myhost.com
sudo /etc/init.d/apache2 restart

7. bring up firefox browser, point it to http://ror.myhost.com/, hola, it works! display ror welcom aboard page.

problem remains:
1. when restart apache2: it complains:
 * Restarting web server apache2                                          
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
[Wed Oct 28 16:05:12 2009] [warn] NameVirtualHost *:80 has no VirtualHosts
 ... waiting apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
[Wed Oct 28 16:05:13 2009] [warn] NameVirtualHost *:80 has no VirtualHosts
                                                                         [ OK ]

2. it's sth complaining about xmlsimple module, when create a new project using rails, it shows:
/usr/lib/ruby/1.8/xmlsimple.rb:275: warning: already initialized constant KNOWN_OPTIONS
/usr/lib/ruby/1.8/xmlsimple.rb:280: warning: already initialized constant DEF_KEY_ATTRIBUTES
/usr/lib/ruby/1.8/xmlsimple.rb:281: warning: already initialized constant DEF_ROOT_NAME
/usr/lib/ruby/1.8/xmlsimple.rb:282: warning: already initialized constant DEF_CONTENT_KEY
/usr/lib/ruby/1.8/xmlsimple.rb:283: warning: already initialized constant DEF_XML_DECLARATION
/usr/lib/ruby/1.8/xmlsimple.rb:284: warning: already initialized constant DEF_ANONYMOUS_TAG
/usr/lib/ruby/1.8/xmlsimple.rb:285: warning: already initialized constant DEF_FORCE_ARRAY
/usr/lib/ruby/1.8/xmlsimple.rb:286: warning: already initialized constant DEF_INDENTATION
/usr/lib/ruby/1.8/xmlsimple.rb:287: warning: already initialized constant DEF_KEY_TO_SYMBOL

-- To fix this one, reinstall rubygems and rails:

wget http://rubyforge.org/frs/download.php/57643/rubygems-1.3.5.tgz
tar xvzf rubygems-1.3.5.tgz
cd rubygems-1.3.5
sudo ruby setup.rb
Once it's done you can remove the .tgz file and erase the rubygems-1.3.4 directory too.then reinstall rubygems:
sudo gem install rails