I'd like to create a custom label for a simple form field. For some reason, the below code isn't creating that label. It's still using the default label. I must be missing something easy.
Simple Form 3.1
<%= simple_form_for "#" do |f| %>
<%= f.input :street, label: "Custom Label" %>
...
<% end %>
How can I create a custom label for my inputs in Simple Form?
You need to use a label helper along with your input helper:
<%= simple_form_for "#" do |f| %>
<%= f.label :street, 'Custom label' %>
<%= f.input :street, as: :string, label: false %>
<% end %>
You can also directly specify input types ie. f.text_field - More info : http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html
You can now pass a label as argument. So the syntax as shown in the question (from 2015) now works:
<%= f.input :street, label: "Custom Label" %>
See the "Usage" section of the readme.
Related
I Have a code in my project like
<div class="form-inputs">
<%= f.input :twitter %>
</div>
It gives label as Twitter in my website. How it produce label with out giving label parameter. I want to change label to something else like Tweet it.
You can add label in very simple way
<%= f.input :twitter, label: 'Tweet it' %>
Check this simple_form usage
If you want to disable label
<%= f.input :input_field_name, label: false %>
Add custom class to label
<%= f.input :field_name, label_html: { class: 'my_class' } %>
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.
Has anybody found a tip how to translate a simple_form input field to upload files ? I have a pretty simple form like that (in slim):
= simple_form_for(#some_object) do |f|
= f.error_notification
= f.input :text
= f.input :photo, as: :file
= f.button :submit
And the generated HTML always has a button 'Choose a file' with a label on the right 'No file chosen'. Googling gave nothing working, most of them proposing tricks in JS. Any other ideas ?
Apparently, there is no easy way to do this. According to the StackOverflow answer, this is not as Rails issue it's an HTML issue.
The way I solved the issue in Simple Form was by adding a label, and hiding the input field.
<%= simple_form_for(#user) do |f| %>
<%= f.label :photo, "Your label here", class: "btn btn-default" %>
<%= f.input :photo, as: :file, label: false, input_html: { style: "display: none" }%>
<%= f.button :submit %>
<% end %>
When tou click the text in the label it will trigger the upload file, opening the window to select the file.
You can then add extra JavaScript to change the label text by the name of the selected file.
I would like to rewrite a form which is used to update a record on a database.
I want to update the form so that the form input does not show the record, as
the record is outputted by the line
<%= q.object.content %>.
I want the
form input not to display the record, and I want that the record is updated
when the input field is edited, and is not edited when it is left blank.
I am new at working with forms and don't know the best way to achieve this.
Can anyone provide any help on achieving this ? Below is the current form. Any help would be appreciated.
<%= semantic_form_for #bunchOfThings do |f| %>
<%= f.inputs do %>
<%= f.semantic_fields_for :aThing, #aThing do |q| %>
<%= q.object.content %>
<%= q.input :content, label: "A Thing: #{q.object.content}" %>
<% end %>
<% end %>
<%= f.action :submit , label: t('Some Text'), button_html: { class: 'btn btn-primary' } %>
<% end %>
You can manually set the default value of a field to an empty string by changing this line:
<%= q.input :content, label: "A Thing: #{q.object.content}" %>
To this:
<%= q.input :content, label: "A Thing: #{q.object.content}", input_html: {value:''} %>
You would also need to filter out blank fields on the backend within the update controller method. Something like this:
def update
filtered_params = permitted_record_params
filtered_params.keep_if{|k,v| !v.blank? }
record.update(filtered_params)
...
end
Where of course the permitted_record_params method returns your permitted params hash.
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 %>