For the last couple of weeks I’ve been working on a small Ruby on Rails application (and learning both Ruby and Rails in the same time). Since everyone’s talking about the one-and-only Rails way, I wanted to make my little app as Rails as possible. That means that you have to write some workarounds to get things done the Rails way.
I wanted to add an onchange event to my collection_select. Normally you would put something like this in your template (that’s what I did after browsing through a couple of tutorial/helps):
<%= collection_select(:story, :license_id, License.find(:all), :id, :name) %> <%= observe_field('story_license_id', :frequency =>; 0, :update => 'story_license_description', :url => {:controller => 'license', :action => 'description'}, :with => '"id="+value') %>
This code however results in ugly URLs (ie. /license/description?id=1). If you want to keep URLs nice, use the following method:
<%= collection_select(:story, :license_id, License.find(:all), :id, :name) %> <%= observe_field('story_license_id', :frequency => 0, :update => 'story_license_description', :url => {:controller => 'license', :action => 'description', :id => "'+value+'"}) %>
This way you’ll keep your URLs beatiful throughout your application (ie. /license/description/1).
Oh, and forgive me the Rails way irony.
0 Responses to “[Hint] Nice URLs with observe_field”
Leave a Reply