Friday, January 20, 2012

Migration Status: How to Determine Pending Migrations

Here is the ticket for determining which migrations are pending against a database:

C:\>rake db:abort_if_pending_migrations

You have 33 pending migrations:
20110921040743 CreateTutorReports
20110922005523 CreateCourses
20110922005721 CreateEnrollments
20110922011612 AddCourseIdToEnrollments
20110922011813 AddStudentIdToEnrollments
20110922012010 AddDepartmentToCourses
20110922012119 AddCourseNumberToCourses
20110922012242 AddSectionToCourses
20110922012404 AddSemesterToCourses
...

Tuesday, December 6, 2011

Looking for make when installing Gem

Sometimes you run into this:


Installing json (1.6.3) with native extensions
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
C:/Ruby192/bin/ruby.exe extconf.rb
creating Makefile

make
'make' is not recognized as an internal or external command,
operable program or batch file.


The gem you are trying to install needs some basic development tools normally found on a Linux/Unix box but not on Windows.

Go here to solve this problem:DevKit for Windows

There are very simple download and install instructions.

Thursday, December 1, 2011

Consuming ID returned by JQuery Autocomplete

This autocompete definition in Participants controller:

autocomplete :participant, :full_name, :display_value => :full_name, :full => true

and this pair of Rails tags:

<%= f.autocomplete_field :name,  
     autocomplete_participant_name_students_path, 
     :id_element => '#participantt_id' %>
  
<%= hidden_field :participant, :id %>

resulting in this HTML

<input data-autocomplete="/participant/autocomplete_participant_full_name"      
  id="full_name" 
  id_element="#participant_id"
  name="full_name" type="text" value="" />  
<input id="participant_id" name="participant[id]" type="hidden" />

does the trick for delivering ID of object found from autocomplete into hidden input field. The value is, then, of course, available as a request parameter when enclosing form is submitted. Your controller accesses this param as such:

Participant.find(params[:participant][:id])


I have tested this in both 0.6.1 and 1.0.4 versions of rails3-jquery-autocomplete gem, using Rails 3.1.1.

Wednesday, November 30, 2011

Gem Versioning

Within Gemfile you can constrain which version or versions of a gem your application is dependent upon.

Any gem inclusion without a version number indicates 'latest' version of gem should be used.

Specifying a gem as such:

gen 'foo', '=3.0'

indicates a specific version must be used.

Specifying a gem as such

gem 'foo', '>= 3.0'

indicates a version >= 3.0 is suitable. In general, you get what you expect from

= Equals version
!= Not equal to version
> Greater than version
< Less than version
>= Greater than or equal to
<= Less than or equal to

Gem versions can also be specified with the 'approximately greater than' operator. This operator increment final digit of a version up until next significant digit would turn over. So, a gem specification of

gem 'foo', '~> 3.1'

is satisfied with any version from 3.1 up to 3.9. inclusive. If three digits are specified in a version number used with ~> operator, as below

gem 'foo', '~> 3.1.1'

then the gem versions compatible with the application are 3.1.1 through 3.1.9, inclusive.

Autocomplete Details

https://github.com/crowdint/rails3-jquery-autocomplete

Wednesday, June 22, 2011

Rails: Calling Controller Action using onclick and Ajax (remote_function)

Say you want to call the update_reasons action in the reports controller in response to an onclick on a checkbox.

First, of course, you need the route in config/routes.rb



match "/reports/update_reasons" => "reports#update_reasons"



And, of course, the action...I just echo relevant arguments in this version.

Note, I render "nothing" because operation does not result in any view change beyond status change of checkbox which is managed by browser.


def update_reasons
  checked = params[:checked]
  params.each do |key, value|
    if /reasons_(\d+)/.match(value) != nil
      logger.debug("update reason #{$1} to #{checked}")
    end
  end
  respond_to do |format|
    format.js { render  :nothing => true }
  end
end


Using the check_box_tag, you will include on :onclick parameter. The value of :onclick is javascript. The "best" way to generate that javascript is using remote_function helper. I use the :with parameter to send request parameters

with the Ajax call. Notice in ruby code above reference to parameter names appearing in value of :with string in view code.



Here is my view code:



<% reasons.each do |infraction| %>
    <% rf =  remote_function(
       :url => { :controller => 'reports', 
               :action => :update_reasons },
       :with => '\'reason=\'+ this.id + \'&checked=\' + this.checked')  %>
    <%= check_box_tag "reasons[#{infraction.id}]", 
      infraction.description,
      false ,
      :onclick => "#{rf}",
      :class=>:"iToggle", :title=>"YES|NO" %>
    <%= infraction.description %>  
  <% end %>



If I expect some server-side generated change to view, I could include an :update argument to remote_function. This parameter names a div into rendered output is placed.

I admit I took a lazy approach by placing value of remote_function to a local variable. After much hair-pulling,I gave on trying to get quotes right with remote_function as direct value of :onclick parameter.

Monday, June 20, 2011

Contact Reasons Associated with a Report

I have added a method to RelationshipToReport model that returns a list of only those 'relationships' that apply to a given report type. The method name is for.

Here is an example usage:

infractions = RelationshipToReport.for(report)
infractions.each do |infraction|
...
end