Rails Forms With Auto-Hyperlink On FKs

Because I use primary_key_prefix_type = :table_name this did the trick for me. Basically, I just set up a hash mapping primary key column names (“InvoiceID” and the like) to the name of the appropriate controller (“invoice_admin”). When outputting a column, if the name of the column is contained in that hash, output a link. All I had to do was write a script to generate the hash and a template for the show.rhtml and Bob was my uncle…

 
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
 
    #lazy initializer for mapping between the names of FK fields and the appropriate controller
    #:return: => nil
    def initialize_identity_columns
        @identityColumns = { 
      'InvoiceID' =>  'invoice_admin' 
      # ... etc ... 
    } 
    end
 
    #called within show.rhtml (probably) ala 
    # <% for column in LineItem.content_columns %>
    # <%= show_column_with_auto_hyperlink(@line_item, column) %>
    #<% end %>
    #:return: => String
    #:arg: currentObj => ActiveRecord::Base
    #:arg: column => ActiveRecord::ConnectionAdapters::Column
    def show_column_with_auto_hyperlink(currentObj, column)
      if @identityColumns == nil
        initialize_identity_columns()
      end
      html = "<p><b>#{column.human_name}:</b> #{currentObj.send(column.name)}&nbsp;"
      optionalLink = nil    
      if @identityColumns[column.name] != nil 
          html += link_to("Show", { :controller => @identityColumns[column.name], :action => "show", :id => currentObj.send(column.name) })
      end
      html += "</p>"
      return html
    end
end

							

Comments are closed.