When using simple_form in Rails, how can I use it only to display data for some fields, and not for input?

I create my forms using simple_form, and all this is good, except when I want to just display some text and not show an input window of any type. So I need to show the shortcut, as well as the displayed text, to go with it, for example. Name: Chris, where β€œName” is the shortcut and β€œChris” is the text on the screen.

So imagine I have 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" 

"f.display_field" is a compiled method, but I understand that the method will look the way I need. All he would do was show a label and some text next to it. What is the easiest way to achieve this?

Greetings Chris

+4
source share
3 answers

I use user input for this purpose:

 class FakeInput < SimpleForm::Inputs::Base # This method usually returns input 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 put it somewhere, as in the application / inputs / fake_input.rb you can use it in your simple forms:

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

The input type is inferred from the name of the input class (without "input", underlined). So, for FakeInput it is: fake.

+10
source

To display some plain text instead of input, just pass the block (and an extra label if it is not an attribute of the model):

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

This simply displays Chris in the same place where the entry would have been different.

0
source

If I understand you correctly:

 =simple_form_for @property do |f| Contact Name: =@property.contact _name =f.input :customer_reference =f.input :premises_description =f.input :po_number, :label=>"Purchase Order Number" 
-2
source

Source: https://habr.com/ru/post/1395870/


All Articles