Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using simple_form in Rails, how can I use it just to show data for some of the fields rather than have inputs?

I'm creating my forms using simple_form and it's all good, apart from when I want to just display some text rather than show an input box of some type. So I need to show a label and also the display text to go with it, e.g. Name : Chris, where "Name" is the label and "Chris" is the display text.

So Imagine I have a simple_form :

=simple_form_for @property do |f|
  =f.display_field "Contact Name",  "Chris"
  =f.input :customer_reference
  =f.input :premises_description
  =f.input :po_number, :label=>"Purchase Order Number"

The "f.display_field" is a made up method, but it is how I imagine the method would look that I need. All it would do Is show a label and some text next to it. What is the easiest way to achieve this?

Cheers Chris

like image 746
cmrichards Avatar asked Feb 11 '12 03:02

cmrichards


Video Answer


1 Answers

I use a custom input for this purpose:

class FakeInput < SimpleForm::Inputs::Base
  # This method usually returns input's html like <input ... />
  # but in this case it returns just a value of the attribute.
  def input
    @builder.object.send(attribute_name)
  end
end

If you place it somewhere like in app/inputs/fake_input.rb you will be able to use it in your simple forms:

= simple_form_for @property do |f|
  = f.input :contact_name, :as => :fake

The input's type is derived from the input's class name (without "Input", underscored). So for FakeInput it is :fake.

like image 198
Simon Perepelitsa Avatar answered Sep 22 '22 19:09

Simon Perepelitsa