Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8433353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:21:48+00:00 2026-06-10T06:21:48+00:00

I am using simple_form and want to override readers for model attributes. Thus following

  • 0

I am using simple_form and want to override readers for model attributes.

Thus following does not work

class C < ActiveRecord::Base

  # it has attribute named pack

  def pack
    "Some value"
  end

end

In view there is following code

<%= simple_form_for @c do |f| %>
  <%= f.input :pack %>
<% end %>

thus it should show form with input box having value “Some value” but it is blank. Why doesn’t override work with simple_form?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:21:50+00:00Added an answer on June 10, 2026 at 6:21 am

    I figured this out, my comment was basically correct, simple_form leans on Rails form helpers which use read_attribute to get the value from an ActiveRecord object thus reading the value in the database and not using your method. A symptom of coupling persistence/domain/presentation. The way around this is:

     <%= f.input :pack, :input_html => { :value => @c.pack } %>
     # or
     <%= f.input :pack, :input_html => { :value => f.object.pack } %>
    

    Or if you want this to be default behaviour you can create your own form builder on top of simple_for such as:

    # lib/my_form_builder.rb **
    
    class MyFormBuilder < SimpleForm::FormBuilder                                                                                               
      def input(attribute_name, options={}, &block)                                 
        options[:input_html] ||= {}                                                 
        options[:input_html][:value] = object.send(attribute_name)                  
        super(attribute_name, options, &block)                                                          
      end
    end
    

    And in your form:

    <%= simple_form_for @c, :builder => MyFormBuilder do |f| %>
      <%= f.input :pack %>
    <% end %>
    

    ** in Rails 3 I don’t think lib is added to the load path by default so you might need to add it and restart you app or put it in app/models (disclaimer not a good idea, lib is better).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm using simple_form for building my forms, this is not a requirement though.
I'm using simple_form gem on a form, and want to use date fields with
I want to add a custom base class for all of my web forms.
I'm using simple_form, and I just want to create association between categories and articles
I'm using simple_form for a form and passing in some URL parameters to prepopulate
I'm using Simple_form with Rails 3.2.1 All fine, except that I need to set
I am using https://github.com/plataformatec/simple_form and am trying to send a extra parameter. I have
I have a simple form that I want to validate using blur if the
I want to create a simple form for adding new products using jqModal .
I'm creating my forms using simple_form and it's all good, apart from when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.