Rails fields_for ignores :disabled field - ruby-on-rails

I'm using Rails 4.1.4 and have the basic structure for nested attributes using fields_for. I've noticed that disabled option is totally ignored for the fields inside the fields_for block.
In my code example the first text_field is disabled and the one after fields_for is not. What am I missing here?
...
<%= f.text_field :response_given, disabled: true %>
<%= f.fields_for :meta_set do |mf| %>
<%= mf.text_field :name, disabled: true %>
...

Try readonly: true instead of disabled.

Related

Rails 6 action text required true not working

I have the following form using action text and trix in Rails 6.1:
<%= form_for :job, url: job_create_path, method: :post do |f| %>
<%= f.label :description, class: "text-muted" do %>
Job Description <span class="lighter"> * required</span>
<% end %>
<%= f.rich_text_area :description, required: true, :placeholder=>"Job Description...", :class=>"form-control", :id=>"description" %>
<% end %>
I have a backend validation that works well. However, I would like to add a frontend validation as well. 'Required true' seems not to work on 'rich_text_area'. Does anybody know how to add a frontend validation as well?
https://github.com/basecamp/trix/issues/328#issuecomment-256788015
Browsers only support the required attribute for native input elements, unfortunately.
As the Trix editor is not a native HTML element it doesn't support required: true. You might have to use some custom JS to validate it.

Why does Rails form multiple: true change submitted param form

I'm playing with ActiveStorage and trying to upload some files locally. Everything works great with the code below, but only if I remove multiple: true from the form. When it is on the form, I get an unpermitted param "files" error in the console. The unpermitted param comes from the way the form is submitting the hash.
Without multiple: true the hash lists attachments as an array (this is the working version):
"article"=>{"files"=>[#<ActionDispatch::Http::UploadedFile:0x007fb4e8e287f0
But with it turned on it it removes the array:
"article"=>{"files"=>#<ActionDispatch::Http::UploadedFile:0x007fb4eb07b7d0
What is causing this form behavior and how can I fix it?
I got the code sample from Engine Yard and here is the project code:
<h3>Attach files to this post</h3>
<%= form_with model: #article, local: true do |f| %>
<div class="form-row">
<%= f.label :file_upload, 'Attach a file' %>
<%= f.file_field :files, multiple: true %>
</div>
<%= f.submit %>
<% end %>
<h3>Attached files</h3>
<% #article.files.each do |file| %>
<%= link_to file.blob.filename, url_for(file) %>
<% end %>
When you use multiple: true you need to permit an array explicit in the article_params for :files:
For example:
params.require(:article).permit(:author, :text, files: [])
You can read more under Action Controller
Good luck!

Form Required Doesn't Work

I have tested three ways of doing a field required, first with no gem, just the usual form_for and it did work well, but I need some good gem for making easier adding fields to insert associations, then I installed the Simple Form. Here is the code I am using:
<%= simple_form_for #post do |p| %>
<%= p.text_field :title, :required => true %> <br />
<%= p.input :content, required: true%> <br />
<%= p.input :category_id, input_html: { required: true }%>
<%= p.submit %>
<% end %>
See how I used all the three ways of getting required to true and the usual way of creating a text field of the form_for so I can see if I find a solution. No success. Even after making config.browser_validations = true in config/initializers/simple_form.rb. Why is it working for form_for but not when I am using gems? I also tried Formtastic and had the same issue.
If you place required: true in the input you should see the field has the "required" class and required="required" attribute.

Using simple form on ruby on rails, how show required (*) asterisk on nested form label

I'm using simple_form, haml, and need a label for a group of nested radio buttons to indicate a selection is necessary via an asterisk.
From the docs:
Required fields are marked with an * prepended to their labels.
By default all inputs are required. When the form object has presence validations attached to its fields, Simple Form tells required and optional fields apart. For performance reasons, this detection is skipped on validations that make use of conditional options, such as :if and :unless.
And of course, the required property of any input can be overwritten as needed:
<%= simple_form_for #user do |f| %>
<%= f.input :name, required: false %>
<%= f.input :username %>
<%= f.input :password %>
<%= f.button :submit %>
<% end %>

Rails i18n and yml structure for form labels

According to the ActionView documentation. Quote:
The text of label will default to the attribute name unless a translation is found in the current I18n locale (through views.labels.<modelname>.<attribute>) or you specify it explicitly.
I have a "user" model and a registration form. Here's a snippet of the relevant part:
<% form_for(#user) do |f| %>
...
<p>
<%= f.label :username %>
<%= f.text_field :username, :class => 'full_width' %>
</p>
...
<% end %>
Dots hide unimportant code.
As I understand the documentation, if I provide a translation in my locale file, in this case :dk, my dk.yml looking like so:
dk:
views:
labels:
user:
username:
"blahblah"
Rails should translate the label text and insert "blahblah" instead of "Username".
This is not happening, so I must have missed something. Any help appreciated.
In Rails 3.1 that is a little bit changed.
<% form_for #post do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.submit %>
<% end %>
en:
helpers:
label:
post:
title: 'Customized title'
I think I found another solution here.
My app was version 2.3.5. I've now changed it to 2.3.8 and <%= f.label :username %> now uses the translation in:
dk:
activerecord:
attributes:
user:
username:
I found the hint in this ticket:
https://rails.lighthouseapp.com/projects/8994/tickets/745-form-label-should-use-i18n
That's because the label method you are calling is not the one from ActionView::Helpers::FormHelper but is in fact the label_tag method from ActionView::Helpers::FormTagHelper. The form_for method is rewriting the code in the given block by adding _tag to the used form helpers. So you're not looking at the documentation for the right method!
I've not yet used that method, as sometimes the label for a field can be different from multiple forms using the same model, so I've written my own helper.

Resources