Don Giulio
Don Giulio

Reputation: 3294

simple form input field as plain text not manually editable

I'm using simple_forms gem to create a form for my model.

The model contains some fields that are the result of javascript calculations based on other fields, so I would like to have them represented as text, instead of an input field, so that graphically they don't seem editable and don't confuse people.

Here's what I have so far in my view:

- field = "time_quantity"
  %strong{:id => "#{field}_str", :class => "text-success"}
    = f.object[field]
  = f.input field, as: :hidden, input_html: {value: f.object[field]}

the text in the %strong writes the value as text, the subsequent input field is what is inserted in the model with the form.

Also the problem with this is that if I input wrong data for these fields I won't be seeing any error notice next to them, because it will be hidden. As shown here:

<strong class="text-success" id="time_quantity_str" data-original-title="" title=""></strong>
<div class="input hidden project_time_quantity field_with_errors">
  <input class="hidden input-medium" id="project_time_quantity" name="project[time_quantity]" type="hidden">
  <span class="error">can't be blank</span>
</div>

I would like to change this to something like:

- field = "time_quantity"
  %strong{:id => "#{field}_str", :class => "text-success"}
    = f.input field, as: :plain, input_html: {value: f.object[field]}

to have more DRY in the view and in the javascript.

How do you think it could be possible to achieve this?

thanks,

Upvotes: 0

Views: 3246

Answers (1)

Rails Guy
Rails Guy

Reputation: 3866

I think either you should use a disabled input or you can use a label field as well.Try like this:

<%= f.input :field_name, disabled: true, hint: 'You cannot change this field' %>

or

<%= f.label :field_name %>

Hope it will help.Thanks

Upvotes: 1

Related Questions