Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Disable protected attributes in ActiveRecordHelper forms (See related posts)

To make the form method in ActiveRecordHelper disable protected attribute, place the following in your application_help.rb:

def attr_protected?(record, column)
  o = instance_variable_get("@#{record}")
  o && o.class.protected_attributes &&
      o.class.protected_attributes.include?(column.name.to_sym)
end

def default_input_block
  Proc.new { |record, column|
    options = attr_protected?(record, column) ? {:disabled => true} : {}
    <<-"end_html" 
      <p>
        <label for="#{record}_#{column.name}">#{column.human_name}</label>
        <br />
        #{input(record, column.name, options)}
      </p>
    end_html
  }
end


And mark some attributes protected, for example:

class Item < ActiveRecord::Base
  attr_protected :created_at
end


The created_at attribute will be rendered but won't be editable. This will work for "dynamic scaffolds" too!

You need to create an account or log in to post comments to this site.


Click here to browse all 4861 code snippets

Related Posts