Ruby Rails: Ajax is not defined error …

This has been a little tricky. I have followed a nice Ajax tutorial for rails but i kept getting the “Ajax is not defined” error in the javascript console. The solution is to include the prototype.js javascript file into your code. Simply write the following into your rhtml file:

 <%= javascript_include_tag "prototype" %>

That’s all.

Ruby Rails: How to add numeric pagination.

Perhaps you are still using the default rails pagination which appears as two links at the bottom of your page ‘Previous page’ and ‘Next page’. If you would like to alter this and be able to access any page instantly you could add the following code into your list.rhtml file:

< %
#here we add the code for enumeration of all available pages
numpage = 1
for a_page in @cert_incident_pages
if a_page == @cert_incident_pages.current
% >
< %= link_to '['+numpage.to_s+']', { :page => a_page } %>
< % else %>
< %= link_to numpage.to_s, { :page => a_page } %>
< %
end
numpage = numpage + 1
end
%>

Don’t forget to change the ‘@person_pages’ to correspond to your variable which is defined in your controller’s list method. Of course you can now remove the ‘link_to’ lines that render ‘Previous page’ and ‘Next page’.

That’s all for now and stay tuned for more code snippets and of course the new release of Firebolt.

Ruby rails: How to render an action and pass parameters to it

The problem is that some things are obvious and others simply seem obvious. Recently i was trying to alter a controller’s create action. By default if creation of the model fails the following code is executed:

render :action => ‘new’

The extra functionality i wanted was to pass the parameter :id into the above action. So i wrote this:

render :action => ‘new’, :id => myvalue

The problem to the above is that params[:id] didn’t get myvalue. After noumerous attempts and no luck searching the internet i came up with the solution.

params[:id] = myvalue

render :action => ‘new’

Trying to rationalize why the above code worked, i came to the conclusion that what ‘render’ does is simply to render the new.rhtml file and not execute the ‘new’ action from scratch. So the code contained in new.rhtml is executed in the context of the ‘create’ method of your controller. It is reasonable then to initialize params[:id] in ‘create’ method and then use its value in new.rhtml.

If the above reasoning is false in any way let me know.
By for now and keep coding. 🙂

Ruby rails: Tutorials about file uploading/downloading in/from a MySQL DB

Check the following 2 links. They have been really helpfull in my effort to add in a ruby rails application the ability to upload any kind of file to a MySql database. In addition downloading has also been rewarding.

Uploading files with Ruby rails in MySql DB
Downloading files with Ruby rails from MySql DB

Note the following problems i came across. In the rhtml file where the uploading form was, i tried to add

:multipart => true in the start_form_tag of this form. Yet it gave bogus html code for some odd reason, i had to comment it out and write the form tag by hand… By the way i use rails 1.0.0

That’s all for now.

Bye and keep coding.

Ruby rails: script/generate scaffold error

While working on a ruby rails project, i stumbled in the following problem. I was trying to create a scaffold for a model (a ruby class representing a row in a DB table). I used the following code:

#script/generate scaffold incident_reported

incident_reported –> is the name of my model

Ruby rails framework makes certain assumptions based on the model name you provide. So in the above example it assumes that my table in the DB is named ‘incident_reporteds’, which of course was wrong. The error i got for running the above script was:

error Before updating scaffolding from new DB schema, try creating a table for your model (IncidentReported)

As a result of the error no *.rhtml templates were produced, which of course was frustrating. I could ignore this and simply create the *.rhtml templates myself. All i had to do was add the following line into my model’s class:

set_table_name(“Incidents_Reported”)

and simply write the *.rhtml files by hand.

Many thanks to Gregory Seidman for the solution he provided me in rail’s mailing list. Here it is:

Toward the end of your config/environment.rb there should be a commented
out section on inflections. You want something like this:

Inflector.inflections do

<span class="moz-txt-tag">|</span>inflect<span class="moz-txt-tag">|</span>

inflect.irregular ‘incident_reported’, ‘incidents_reported’
end

I have tested it and it works just fine 🙂
Keep coding.