Search code examples
ruby-on-railsactivescaffold

ActiveScaffold hide fields on update form based on record data


I have a Rails app that uses activescaffold and I would like to hide some fields on the update for some of the records.

I have been trying to do it using helper methods, but I can not seem to get that to work.

What is the best way to do this?


Solution

  • The best way to do this is by using one of the security method templates (depending on your need) provided by activescaffold plugin.

    Pasted from activescaffold wiki:

    Model Methods: Restricting Anything Else

    On your model object you may define methods (none of which accept any arguments) in any of four formats, depending on your need for granularity.

    The formats are:

    * #{column_name}_authorized_for_#{crud_type}?
    

    For example if you have an activescaffold based controller called user:

    class Admin::UsersController < ApplicationController
      active_scaffold do |config|
        config.columns = [:username, :name, :email]
      end
    end
    

    And you only want to allow the user to be able to update the username if they are admin then you can do something like this:

    User Model:

    class User < ActiveRecord::Base
    
      # ActiveScaffold security template: #{column_name}_authorized_for_#{crud_type}?
      def username_authorized_for_update?
        # As soon as this method will return false 
        # the username field will not be available on the update form
        return true # Write logic to decide if username field should be visible
      end
    end
    

    Active Scaffold wiki link: https://github.com/activescaffold/active_scaffold/wiki/Security