dependant dropdowns (select menus) using rails

There are multiple solutions on the net for dynamic select menus. By saying “dynamic menus” I mean that we have a form with two select dropdowns. When we change the first one, the content of the second one is updated.
The most promising that I have found are:
A. http://railscasts.com/episodes/88-dynamic-select-menus
This one includes writing JS code. I don’t like writing JS code.
B. http://github.com/splendeo/dependent_select
This one does not use AJAX. It generates the whole thing in JS and sends it to the client. Can be useful when dealing with not big data.
C. http://pullmonkey.com/2008/3/30/dynamic-select-boxes-ruby-on-rails/
A very nice one but in my opinion a bit complicated for a beginner.

I had to develop my own solution. This is what I came up with. Lets assume we have a form for Foo model that has a select dropdown with Bar1 model and select dropdown with Bar2 model, that is dependent on Bar1 current selection.
A. In the Foo model form, add a field observer:

  <%= observe_field :foo_box1_id, :url => { :action => :box2_select_box },
      :update => :foo_box2_id,
      :with => :box1_id
      %>

B. In the FooController add:

  def box2_select_box
    @box1 = Box1.find(params[:box1_id]) unless params[:box1_id].empty?
    render :layout => false
  end

C. In the view for the above action (box2_select_box.html.erb) put:

<%=
if @box1
  options_from_collection_for_select(@box1.box2s, :id, :name)
else
  options_for_select([["Nothing to select", ""]])
end
%>


No JS code writing. Working with AJAX. Simple.

EDIT:
In reply to Rob Bean’s comment (thanks!) I attach a full rails project with a sample.
The “@box1.box2s” comes from models association. box1 has_many box2s (see box1.rb)

Posted by wojtek Thu, 12 Nov 2009 22:37:00 GMT


ntldr is missing vista ubuntu grub

After reinstalling my Windows Vista I had to recover Grub menu. I tried https://help.ubuntu.com/community/RecoveringUbuntuAfterInstallingWindows and EasyBCD. It was not working. I downloaded Ubuntu 9.10 CD and boot it, hoping to do a grub-install. After reboot I got my Grub menu and when trying to boot Vista a “NTLDR is missing” error.

The solution that worked for me was to:
FIRST OF ALL:
Follow http://neosmart.net/wiki/display/EBCD/Recovering+the+Vista+Bootloader+from+the+DVD
SECOND OF ALL:
Follow https://wiki.ubuntu.com/Grub2#Recover%20Grub%202%20via%20LiveCD
And watch out for the menu.lst generated (/boot/grub/menu.lst), mine included thinks like:

title Ubuntu 9.10, kernel 2.6.31-14-generic
root 
kernel /boot/vmlinuz-2.6.3........
initrd /boot/initrd.img-2.6.3........
quiet

But it should be:

title Ubuntu 9.10, kernel 2.6.31-14-generic
root (hd0,4)
kernel /boot/vmlinuz-2.6.3.......
initrd /boot/initrd.img-2.6.3.....
quiet

It seams that update-grub when doing a chroot generated wrong partition numbers for grub. To be more specific it did not generate any partition pointers for the loader :) But after changing those everything works fine.

(hd0,4) is a grub partition number. if you don’t know how to find out what to type there study grub manual http://www.gnu.org/software/grub/manual/html_node/Naming-convention.html
Mine is:
hd - hard disk
0 - disk no. 1
4 - first extended partition

Posted by wojtek Wed, 11 Nov 2009 13:13:00 GMT


rails integration tests hardcoded routes problem

We had a problem with hard-coded routes in integration tests while using mixed RESTful and default routing. For example we can have in a integration test:

test "my sample test" do
  get 'foo_controler/bar_action'
end

This looks not too nice, a string-hard-coded route to controller and its action.

The quick and fair solution we found was this one: First of all: do not use default routes, use RESTful routing only (see http://www.slideshare.net/ihower/rails-best-practices)

So we comment these things in routes.rb:

  #map.connect ':controller/:action/:id'
  #map.connect ':controller/:action/:id.:format'

Second of all: use named routes instead of the default ones, so we add to routes.rb:

map.foo_controler 'foo_controler/:action/:id', :controller => :foo_controler

And change the integration test to:

test "my sample test" do
  get foo_controler_path(:action => :bar_action)
end

Let me know what you think about that.

EDIT:
I am still learning much rails.
I came across another problem and found the “:only” parameter for the map.resources. This means that one should not use named routes. Only RESTfull routes should be used and the inclusion of the methods you want to implements is done by “:only” as the API says
This example is copied form the API reference:

  map.resources :posts, :only => [:index, :show] do |post|
    post.resources :comments, :except => [:update, :destroy]
  end

Example of adding new, non-standard, routes:

  map.resources :users, :collection => {
    :show_profile => :get,
    :edit_profile => :get,
    :update_profile => :post
  }  

So the above integration test will look diferent. The helper methods can be found by listing the routes with “rake routes”.

Posted by wojtek Mon, 09 Nov 2009 20:16:00 GMT


Older posts: 1 2 3 4 5 ... 24