Topic: Checking strings for certain contents

I have adapted the standard Rails scaffold in such a way, that I can pass various Models over to my controller so that it generates the correct views.

Yet there is a certain problem I can't solve with only applying my rather humble Rails knowledge...

If I create a view dynamically to insert a new object into my database there have to be created the right text fields and text areas for the columns in my database. The according code looks like this:

<% form_tag :action => 'create', :model => @model_name do %>
  <% @columns.each do |c| %>
    <% if c.type == :text %>
      <p><label for="<%= @model_name+"_"+c.name -%> "><%= c.name.capitalize %></label></p>
      <%= text_area @model_name, c.name %>
    <% else %>
      <p><label for="<%= @model_name+"_"+c.name -%> "><%= c.name.capitalize %></label></p>
      <%= text_field @model_name, c.name %>
    <% end %>
  <% end %>
  <%= submit_tag "Create" %>
<% end%>

Now there is the problem that if there is found an ID column (such as foreign keys) it is displayed in the view so that I can manipulate it from the view. In the default Rails scaffold this doesn't happen, because the static form created by it doesn't include any ID related fields (I don't know though how it checks for them).

How can I prevent this from happening?

I have to check the string coming from the database if it ends with '_id' or simply is called 'id'. The latter should be easy, the former I don't know how to accomplish.

Re: Checking strings for certain contents

Hi again Stalafin,

I assume @columns is fetched using the .columns method? Try using the .content_columns method instead, that should filter out all id fields (this is how scaffold does it).

Alex