How to change label of simple form input - ruby-on-rails

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' } %>

Related

Rails and Simpe Form - translate file upload button

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.

Rails: Simple Form Custom Label Not Working

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.

Semantic ui with simple_form

tell me how to configure simple_form. I would like to use the checkbox of semantic ui, but when I wrap checkbox in the class, he becomes active, that is, visually it is, but when you click the check box is not activated.
= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f|
.ui.three.column.middle.aligned.relaxed.grid.basic.segment
.column
.column
.column
.ui.form.segment
#marg.field= f.label :email, 'Email'
#marg.field=f.input :email, placeholder: 'Email', autofocus: true, label: false
#marg.field= f.label :password, 'Пароль'
= f.input :password, :required => false, placeholder: 'Пароль', label: false
#marg.ui.toggle.checkbox
= f.input :remember_me, as: :boolean if devise_mapping.rememberable?
#marg= f.button :submit, 'Войти!', class: 'small ui blue submit button'
http://i.imgur.com/C8Wn4K9.png
Please try it
= f.input :remember_me, as: :boolean, boolean_style: :inline
A simpler way to do this is to create a custom simple form wrapper. Here is a blogpost which describes how to add the wrapper: http://pranavsingh.me/semantic-ui-simple-form-wrapper/
The above configuration will automatically add all the essential classes semantic form classes, and also adds wrappers to create proper checkbox fields. Below is an example:
= f.input :published, label: "Published?",
hint: "If you are not ready to go live yet, keep this site unpublished.",
wrapper: :ui_toggle_checkbox
Here's what I had to do (the accepted answer got me on the right track, but didn't fully fix the issue):
In the config/initializers/simple_form.rb file, change
config.boolean_style = :nested
to
config.boolean_style = :inline
This prevents Simple Form from wrapping the input tag in the label tag (essentially inserting a tag between the '.ui .checkbox' div and the input, which breaks Semantic's checkbox functionality).
In the app/assets/javascripts/application.js file:
$('.ui.checkbox').find('input').checkbox();
My form now displays checkboxes correctly and passes the appropriate value when submitted.
Here is another option (to just fix one checkbox) -
In a _form.html.erb partial:
<div class="inline field">
<div class="ui checkbox">
<%= my_form.input :my_input, label: 'My Label', as: :boolean, boolean_style: :inline %>
</div>
</div>
In the application.js file:
$('.ui.checkbox').find('input').checkbox();

Rails simple_form label for select

I'm trying to set a label for the following line but I keep getting an error if I add label_method anywhere. label: is just ignored. How can I add a label for f.select?
<%= f.select :state_identifier, Location::STATE, { prompt: 'State', id: 'state' } %>
I tried the following but it doesn't format properly in form-horizontal, leaving no gap between the label and data.
<%= f.label :state_identifier, label: 'State' %>
This is because f.select is not a simple_form method and does not support :label
Something like this should work for you w/ simple form.
<%= f.input :state_identifier, :label => "State", :collection => ["a","b"], :input_html => {:id=>"state" } %>
Hope this helps.

How to make the body textarea of form pre filled in Rails?

I have a simple form
<div class="form-inputs">
<%= f.input :title %>
<%= f.input :body %>
</div>
now I want the textarea to be already pre filled with some text (preferably html), how to do it ?
Thanks
You just want a value to be filled in then use this:
<div class="form-inputs">
<%= f.input :title, :input_html => { :value => "This is title field value." } %>
<%= f.input :body, :input_html => { :value => "This is body field value." } %>
</div>
There is too few details in your question. There is an option for input method to specify a value. But I'd say it is not very good practice.
The good practice would be:
for existing record show what it has in body: it already works
for a new record, if you want default body text, you'd better specify it on creation
#rec = Record.new(:body => "default text")

Resources