Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert Column.is_foreign_key? ...

Replytotopic

Column.is_foreign_key?

Posted in Forums : Ask a Rails expert

 
Profile

Authority 0
Posting Rating 0
Sign in to rate this post

I’m trying to do a simple general form creator where i can just specify the table to create the form for, the controller/action wich will handle the submit and a small title, so far it works great, but only adds textfields.

My question is, is there a way to know if a column is a foreign key (other than checking if the field ends with ‘_id’. I want this so if the field is a foreign key i can present a drop down menu from the other table instead of the text field.

So far i have this…

Controller

class SharedController < ApplicationController def form_for @for = params:locals.camelize.constantize.new @cols = params:locals.camelize.constantize.column_names @ctrl = params:locals @action = params:locals @title = params:locals render(:partial=>‘form_for’) end
end

The view is actually a partial and looks like this
<%= @title %>

<% form_for @for, :url => { :controller => @ctrl, :action => @action } do |f| %> <% @cols.each do |c| %> <% if c != ‘id’ %> <%= f.label c %> <%= f.text_field c %>
<% end %> <% end %> <= f.submit “Guardar” %>
<
end %>

And a example call to it is

<%=link_to_remote(”+”, :update => ‘form_div’, :url => { :controller=> ‘shared’,:action => ‘form_for’, :locals => { :title => ‘Idioma Nuevo’, :for => ‘language’, :ctrl => ‘params’,:action => ‘save_language’, }})%>

wich in that case will put the partial on a div.

any help would be apriciated, or comments on the way i’m doing this, first time using rails ^^

Replytotopic

Other Recent Topics

Ask a Rails expert : nested application ApplicationController get called intead of children::ApplicationController

Ask a Rails expert : Best way to structure a database for a large/static dataset

Ask a Rails expert : Ruby Developer (ROR) - Scottish based (Remote working from within the UK)

Ask a Rails expert : Above Ground Pool Supplies

Ask a Rails expert : How to get url params in observer or model in Rails 3.1

Ask a Rails expert : What can persuade you to hire Junior Ruby devs with significant PHP experience?

Ask a Rails expert : What industry value does the Ruby or Rails Certification currently have?

Ask a Rails expert : Louis Vuitton Damier Azur Canvas specially sale ( www.salecheaplouisvuitton.com )

Ask a Rails expert : ·How to check errors/puts statements from ruby files which are under cronob

Ask a Rails expert : Louis Vuitton cheap Soft Sided Luggagespecial offer( www.salecheaplouisvuitton.com )

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel