0

I have a table in my Ruby on Rails 4.1.4 application and I want to add another column that should be read-only in the view and can be changed explicitly via SQL query from the code only.

How can I achieve such behavior?

Thanks in advance.

FrozenHeart
  • 19,844
  • 33
  • 126
  • 242

1 Answers1

0

Assuming you're using strong parameters, you can remove the column from the list of strong parameters in the controller.

Here's some example code from the strong_parameters github repo to know if you're using strong parameters:

class PeopleController < ActionController::Base
  # This will raise an ActiveModel::ForbiddenAttributes exception because it's using mass assignment
  # without an explicit permit step.
  def create
    Person.create(params[:person])
  end

  # This will pass with flying colors as long as there's a person key in the parameters, otherwise
  # it'll raise an ActionController::MissingParameter exception, which will get caught by
  # ActionController::Base and turned into that 400 Bad Request reply.
  def update
    person = current_account.people.find(params[:id])
    person.update_attributes!(person_params)
    redirect_to person
  end

  private
    # Using a private method to encapsulate the permissible parameters is just a good pattern
    # since you'll be able to reuse the same permit list between create and update. Also, you
    # can specialize this method with per-user checking of permissible attributes.
    def person_params
      params.require(:person).permit(:name, :age)
    end
end

The stuff in person_params is what strong_parameters does.

animatedgif
  • 1,060
  • 12
  • 16
  • How to know whether I'm using strong parameters? – FrozenHeart Sep 24 '14 at 13:37
  • @FrozenHeart google "rails strong parameters" and see if you're using it in your controllers.... updating answer to include example of strong parameters in controller. You probably are using it because IIRC it comes with Rails 4 – animatedgif Sep 24 '14 at 13:38
  • Unfortunately, I can't understand what should I do in this case. I'm using Active Admin and I don't see any code like this – FrozenHeart Sep 24 '14 at 14:10
  • You didn't mention you were using active admin... It looks like you should be able to mark the fields as disabled in your view. Is this what you're looking for? http://stackoverflow.com/questions/12703987/how-to-display-only-the-value-in-edit-page-in-active-admin – animatedgif Sep 24 '14 at 18:22
  • @FrozenHeart Hey if this answer helped you out, it would be awesome if you could mark it as the "accepted" answer. Thanks a lot! – animatedgif Feb 29 '16 at 20:16