Thursday, December 10, 2009

HTML alternative link to multiple active-record models

The problem :

I needed to display a html link to an active-record model and a specific action in the respective controller only if a given condition was met. I tried to use the rails link_if helper but it was not suitable because I wanted to perform the test on several alternative conditions leading to several alternative links.

In fact, I was building an audit table listing errors coming from several active-record models. I wanted to jump back from the error in the audit table to the original record in the table producing the error. I had one audit table and 6 different tables where the error could come from. In the audit table I had 2 special fields. The first one representing the original ID from the table producing the error. The second one representing the source of data, i.e. the table producing the error.

The solution:

Using the CASE syntax in RUBY I was able to circumvent the problem which puzzled me for a while.

The correct syntax in the view is the following:

<% case  validation.OutputFile 
        when "LIMIT" %>
           <%= link_to image_tag("icons/application_edit.png") , :controller => 'limits' ,:action => "show", :id => validation.Original_id %>
        <% when "COUNTERPART" %>
           <%= link_to image_tag("icons/application_edit.png") , :controller => 'counterparties' ,:action => "show", :id => validation.Original_id %>
        <% when "PROVISIONS" %>
           <%= link_to image_tag("icons/application_edit.png") , :controller => 'provisions' ,:action => "show", :id => validation.Original_id %>
        <% when "NOSTRO" %>
           <%= link_to image_tag("icons/application_edit.png") , :controller => 'nostros' ,:action => "show", :id => validation.Original_id %>
          <% when "OTC" %>
           <%= link_to image_tag("icons/application_edit.png") , :controller => 'derivatives' ,:action => "show", :id => validation.Original_id %>
          <% when "LOANBOOK" %>
         <%= link_to image_tag("icons/application_edit.png") , :controller => 'loanbooks' ,:action => "show", :id => validation.Original_id %>
        <% else %>
         <%=h "-" %>
          <% end %>


Where : 'nostros', 'limits', ... are the names of my controllers where I want to perform the action 'Show' on the original id (id in the table producing the error). "OTC","LIMIT","LOANBOOK",... are the values in the field OUTPUFILE in my audit table indicating the source of data producing the error. validation.OutputFile is the value being evaluated in the WHEN condition.

At the end if no condition is met, I have chosen to display '-' instead of the image link. You could also choose not to display anything or a blank value ''.

Simple and efficient.

Saturday, November 21, 2009

Ext JS 3.0 Cookbook

Quick answers for common problems. A new book was published by Jorge Ramon for Packt Publishing. It delivers 109 great recipes for building rich apps using the Ext JS Javascript library.

More

ISBN of the book is : 978-1-847198-70-9

Sunday, June 28, 2009

Rails view: Export to pdf

This method exports html views in PDF using the exact same CSS and HTML you display on your site. This means that you don't need an extra effort in reformating everything to look nice on a PDF file.

This tutorial is mainly based on the method described by Jim Neath (site) with the necessary windows tweak.

1. Install

Download and install PrinceXML

Install the princely plugin (interface between Prince and rails)

 script/plugin install git://github.com/mbleigh/princely.git

2. Tweaking for Windows

The following trick is necessary to make it work under Windows.

Change the path of the application in the file \vendor\plugins\princely\lib\prince.rb with the following line :

@exe_path = "C:\\Program Files\\Prince\\Engine\\bin\\prince" .chomp

the old line was : @exe_path = `which prince`.chomp

Add

pdf.binmode #this is new

under the line where you see:

pdf = IO.popen(path, "w+")

That should be ok after this.

3. Usage

In your views:

Copy the view you want to print, modify it to display only what you want to print (using CSS and HTML like a normal view) and rename it (example : show.pdf.erb instead of show.html.erb)

In your controller:

edit the corresponding action to add a new render method for the PDF file

# GET /post/1
# GET /post/1.xml
def show
@post = Post.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml  { render :xml => @post }
format.pdf do
render :pdf => "filename", :stylesheets => ["application", "prince"], :layout => "pdf"
end
end
end

That's it

When you call, /posts/show/5.pdf you get a pdf file of the record id you have called.
In my example, the record id was 5.

More information
Thanks to mikedc55 for the tweak on windows.

Rails view: Export to csv

A simple and efficient export of a view to a csv file can be achieved by following these steps:

1. Install

install the following gems:

gem install fastercsv
gem install crafterm-comma --source=http://gems.github.com


2. Usage


In your controller, add:

class PostsController < ApplicationController

# GET /posts
# GET /posts.xml
# GET /posts.csv
def index
@posts= Posts.find(:all)

respond_to do |format|
format.html # index.html.erb
format.xml  { render :xml => @posts}
format.csv { render :csv => @posts}
end
end

In your model, add:

# ===============
# = CSV support =
# ===============
comma do  # implicitly named :default


field_to_display_1
field_to_display_2
field_to_display_3
field_to_display_4
field_to_display_5
field_to_display_6

end

In your view, add a link to the file:

the text is for example "Export to Excel" and the target of the link is /posts.csv

Rails will see the different extension and choose the appropriate render method from your controller.


3. That's it


When you click on the "Export to excel" link in the view you will get a CSV file. Depending on your browser settings, it will open in MS Excel or ask you where to save it.

from there, you can extrapolate and add params to the view.

More information

Saturday, June 27, 2009

Ext JS 3.0 and rails

Ext JS 3.0, the javascript framework, is now reaching beta 2 with a REST support. This could play along quite well with rails.

Go check it out here

Wednesday, April 8, 2009

Questions on SQL Server adapter

Just a brief post to note that there is a specific discussion group on SQL Server adapter in rails here.

Thursday, January 1, 2009

Learning Ext JS

Packt Publishing recently published a new book Learning Ext JS

This book is well written and examples are easy to follow. although they are using PHP for most of the examples, it is fairly easy to convert to rails. It is a very good and time saving introduction to the Ext JS framework before digging into the official documentation.

Ext Scaffold returns

In a previous post, I spoke about Ext Js scaffolding with the plugin written by Martin Rehfeld

The exact description is the following :

The Ext Scaffold Generator Plugin is a drop-in replacement for Rails’ standard Scaffold Generator. Accepting the very same options, it will generate views using data grid and form components from the Ext JS Javascript GUI framework as well as a controller acting as an Ext-compatible JSON web service. The generated code can be used as a starting point for further implementation and outlines solutions on how to integrate the Ext JS library with Rails as a backend.


you can now find the plugin on github

and install it in your application like this :

ruby script/plugin install git://github.com/martinrehfeld/ext_scaffold.git

More info on GL Network

The advantage of this new version is that all actions happen in the same window (in your index view). The javascript code is now explicit and does not use magic helpers anymore. This is a great improvement. It allows an easy customization of the code and is a great resource to learn how to interact with the Ext JS framework and rails. It also facilitates integration in your current application.