Archive

Archive for the ‘Ruby on Rails’ Category

Setting method parameter in form_remote_tag in RESTful setup

June 16, 2007 6 comments

By now, I gotten used to reading the codes if the Rails api documentation is not clear enough.

If you are using Rails in a RESTful setup, and using form_remote_tag, or any other methods that generate a AJAX request, then you might encounter this problem.  RESTful requires you to understand that to get a listing of information, you’ll need to use a HTTP GET method.   If you do not specify the method during the form_remote_tag call, the default HTTP method is POST.  So when the AJAX request is called, the Rails engine will interpret it to be a “Create” call, and the “create” method in your controller is used.

You will get a nasty mess…

Actually the way to set the method in your AJAX request is quite simple.  Just include :method => ‘get’ in your options hash.  For example:

form_remote_tag :url => {:action => “index”}, :html => {:id => “people_filter_form”}, :method => ‘get’

My problem was to put it in the :url hash, or :html hash.  Won’t work. 🙂

Categories: Ruby on Rails

Reserved words in Rails

May 30, 2007 15 comments

Oh the agony… I just spent half a day agonising over a new model that I created, and didn’t work when I was doing a find for a record. Just because one of the column name is a named after a reserved word! The error was an obscure:

SyntaxError: compile error
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/base.rb:1359: syntax error, unexpected tINTEGER
Object::1

So I dedicate this blog entry to the listing of reserved words in Rails. If anyone else has come across any other reserved words that gave you hell, please comment!

Reserved words for Rails’ ActiveRecord
  • type
  • integer
  • string
  • text
  • file
  • number
Reserved words for Rails’ ActiveController
  • template ( Someone mentioned this on the Internet. I didn’t test this out )

Updates:

Some people have pointed me to these links.  They are more comprehensive!

Creating a record with your intended id

May 7, 2007 Leave a comment

In Rails, you can create a new record with the methods create; or new followed by save.  In both cases, the id of the record is automatically allocated.   This is how you can allocate your own id in a new record.  Why do we need that?  Well, I did, and someone probably need too.  For me, it’s a long story.  So I’ll just go into the How-to.

[Peep Code]

    s = Status.new(:name => ‘New’)
s.id=Status::NEW
s.save

[End of Peep Show]

Now you know why? 🙂

Categories: Ruby on Rails

RadRails and Aptana

March 31, 2007 Leave a comment

RadRails, the Eclipse plugin for Rails development, has been handed over to Aptana to continue in its development.

These were Kyle’s words (taken from Google’s cache) before radrails.org went into oblivion:

by Kyle – 03/08/2007 09:48 PM

Earlier this week I wrote about the challenges facing RadRails. I chose to come out and face our problems head on rather than hide from the obvious. Matt, Marc and I originally developed RadRails just for our own use; we wanted a Rails IDE. Who knew that so many people would show up after our incredibly weak 0.1 release. We were compelled to meet the abundant demand and set off on a tireless mission to bring a free Rails IDE to the masses.

Since that time we’ve traveled the world, spoken at 6 conferences in 4 countries promoting a different kind of web development and approach to tooling. We’ve set a trend for Rails specific tooling that can now be seen in other IDEs such as Netbeans and IntelliJ. In many ways we’ve accomplished what we initially set out to do and 18 months later there is not just one Rails IDE, but close to a dozen free solutions.

Yesterday at EclipseCon I announced that Aptana would be taking over the RadRails project and integrating it into their existing suites of editors, debugging tools and community features. Rails tooling on Eclipse needs a full-time effort and Aptana has the team and resources to do just that.

The decision to transition RadRails to another group hasn’t been easy. We have poured ourselves into this project and letting it go is tough. After meeting with Aptana in person it became clear that they are really committed to their product and open source community. Paul and his team have the ambition and vision to take Rails and their Web 2.0 tooling to the next level. We will be working closely with Aptana to provide a smooth integration over the next few months and hope to have things completely transitioned by mid May.

I’d like to thank our community of users, especially those that have been behind the project since the beginning, for validating our work. We are going to need your help in merging these communities and assisting existing Aptana users with our Rails tooling. Please join the Aptana forum and resources to make this process as seamless as possible. Documentation will be the first hurdle and I hope our community can step up to help.

Aptana will continue to keep it open source (Yeeaahh!). The official website for RadRails is now http://radrails.net. The old site’s domain is already not available.

You can see from the new site that there’s a lot of activity in the development of RadRails. That’s really good!

Thank you for making software development available to the masses.  Keep up the good work guys, and keep the open source movement going!

Categories: Ruby, Ruby on Rails

has_many’s build method and dates

March 21, 2007 Leave a comment

Let’s say you have a HTML form with a date field, and after submitting it, you will be passing the parameters into your ActiveRecord class’ build method (made available by the inclusion of :has_many) to create an instance of the class.

If your date does not have the “day” component, then the build will return an instance of the class with the date variable as a nil.

Therefore, make sure that your date has a “day” component if you are using a text input field for your date in the HTML form.  Or rather, make sure that your date has all the components.

If you are using a date_select with only the month and year select lists in the HTML form, you are still safe, because Rails will still have the day component (set to today) included in the request’s parameters list.

I am sorry if none of this makes sense to you. But it will if you have been trying to make your dates work with the model class’ build method.

Just a last, and hopefully understandable statement, if you are using the build method for your model class, and there is a datetime variable, then make sure that the date is complete with day, month and year. Otherwise you’ll get a nil for the datetime variable in the class returned.

Categories: Ruby on Rails

DHTML Calendar and ModalBox

March 20, 2007 6 comments

If you have been trying to do a calendar pop-up within a form encapsulated around a modal box, then you are likely to encounter this problem where by the calendar doesn’t pop up.

Before we go any further, the software I’m using are:

The reason why it doesn’t pop-up, or rather it actually does, is that if you observe the modal box carefully, there are actually 2 layers.  The bottom smoked-out layer is the parent document.  When the modal box appears, that element’s z-index (set at 10000 by ModalBox) is brought up higher than the parent document, hence layered above the parent document.  When you click on the calendar link, it’s z-index is equal to the parent document’s, hence below the modal box.  Which means, the modal box is covering it, so you couldn’t see the calendar, even though it’s there.

If you set the calendar’s z-index to be equal to the modal box, the other problem will be its position.  The modal box’s position is set to “fixed”, so when you scroll the page, the form encapsulated by the modal box doesn’t move, but the calendar and the parent document moves with the scrolling.  Therefore, IF your link which opens the form-in-modal box is at the bottom of a long page, your calendar pop-up can be at the top of the page, which means you won’t be seeing it too.

Therefore, in order to have a calendar pop-up in the form-in-modal box, your calendar’s css style needs to set the position to “fixed, and the z-index to “10000”.  This will require you to change the codes in DHTML calendar.

I am currently trying to get in touch with Mihai Bazon.  Hopefully I can contribute my modifications to his product, so that everyone else can use it too.

Multiple select of checkboxes

March 19, 2007 6 comments

This is Rails related. I am not sure why this was not covered in any of Rails-related manuals, tutorials or API documents. Maybe it is, somewhere out there in the wild, but it sure took me a long time to figure this one out. A chance analysis of sample code, remotely related to a checkbox, by another person crying for help because his code was not working, gave me a lead in how to get this working.

So if you are trying to do a multiple select of checkboxes in your form, but when you submit the form, only one value was available in your controller, then here’s the solution.

In your rhtml file, your checkbox code should be:

<%= check_box_tag ‘item[]’, item.value%>

Note the “item[]”.  This will become the checkbox’s value name.  But it’s the [] that will be interpreted into an Array type, and the selected values populated into an Array.  The “item.value” is to be replaced by whatever value you intend to put in.

At your controller, you can retrieve the array of values by:

values = params[items]

There you have it.  Multiple select of checkboxes. 🙂

Categories: Ruby on Rails

Rails and observe_form

March 8, 2007 Leave a comment

In using the method observe_form in Rails, your form should be created with the option :id.  Otherwise, the observe_form won’t be able to locate the form and javascript error is encountered in prototype.js.

Sample code snippet in the rhtml:

<% form_tag(‘javascript:void(0)’, :id => ‘my_form’) do %>

<% end %>

<%=  observe_form :my_form,
: url  => {:action => ‘new’} %>

Categories: Rails Reference

Bug in Rails’ observe_field and prototype.js

March 8, 2007 1 comment

I tried using Rails’ observe_field yesterday, and it didn’t work if you specified the :frequency option with the value set to less than or equal to 0.

Code snippet:

<%= observe_field :item_id,
:frequency => 0,
:update => “this_part”,
:url => {:action => ‘new’},
:with => “‘item_id=’ + value” %>

You would have got a javascript error in prototype.js at

 onElementEvent: function() {
var value = this.getValue();
if (this.lastValue != value) {
this.callback(this.element, value);
this.lastValue = value;
}
}

It would seems that nothing was happening if you weren’t on the look out for javascript errors.  No big issue.  Just remove the :frequency option and you are good to go.  The frequency option works fine for values greater than 0.

My version of Rails: 1.2.2

Categories: Bug, Ruby on Rails