Rails Form: Placeholder Text Ignored - ruby-on-rails

This line is in one of my forms:
<%= question.answer %><%= f.text_field :answer, :placeholder => "Respond..." %>
It displays the answer to a question and shows a text field to update that answer.
The only problem is that the place holder text is never shown and the content is always set to the answer content rather than "Respond..." as a placeholder.

Try this:
<%= question.answer %><%= f.text_field :answer, '', :placeholder => "Respond..." %>
You were setting your placeholder as the value parameter: text_field_tag(name, value = nil, options = {})
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-text_field_tag

<%= question.answer %><%= f.text_field :answer, :value => "", :placeholder => "Respond..." %>
Does the trick it seems, this is a modified version of #Yogzzz's answer - he should get credit. He hasn't made the changes to his answer so I can't accept it as correct.

Related

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")

Disable a Text Box in Ruby on Rails?

I have the code:
<% generate_bullets = Bullet.all %>
<% generate_bullets.shuffle.first(4).each do |t| %>
<%= f.text_field, :bullets, :class => 'text_field disabled' %>
I want to disable a text box using embedded ruby, and am unable to do so. If I could receive any help on the situation I'm facing, it would be very greatly appreciated.
After I disable the text box I want to have a button generate four random ID's from the database table "bullets" and print them on the disabled text box in an array format, and utilize those four printed ID's to post them onto a created page. Any help with that would be even better.
Let me know if I'm reading this right: you're trying to disable the text field from the get-go in the HTML. Is that right?
If so, disabled isn't a class; it's its own attribute.
<%= f.text_field, :bullets, :class => 'text_field', :disabled => true %>
You can also use :readonly => true attribute.
For HAML
= f.text_field :name, :class => "form-control", :readonly => true
For ERB
<%= f.text_field :name, :class => "form-control", :readonly => true %>

Rails form text field helper, how to make the input area wider?

How do I make the text field area wider?
I tried:
f.text_field :title, size => 150
I also tried width, I am missing something here, what is it?
I think it should be
f.text_field :title, :size => 150
Or, you can add :class option and use css to define the size (I prefer)
You can also do something like:
<%= f.text_area :description, :cols => "10", :rows => "10" %>
Are you using the same in your code. I think you are missing a colon before the size.
<%= f.text_field :title, :size => 150
%>
or you can use
<%= f.text_field :title, "size" => 150
%>
size is an undefined local variable whereas :size and "size" are passed as options to the text field form helper
You can do it by simply using rows option in your text field. Like
<%= f.text_area :fieldname, :rows => "10" %>
Size should work, could you post a chunk of code?
FormHelper Documentation
You can always specify a class using the class symbol and specify a width using CSS
<%=text_field_tag 'some_input', nil, :class => 'some-class'%>
Try this for the input field
<%= f.input :content, as: :text, input_html: { rows: "2" } %>

Ruby on Rails: How do I add placeholder text to a f.text_field?

How can I add placeholder text to my f.text_field fields so that the text comes pre-written by default, and when a user click inside the fields, the text goes away - allowing the user to type in the new text?
With rails >= 3.0, you can simply use the placeholder option.
f.text_field :attr, placeholder: "placeholder text"
In Rails 4(Using HAML):
=f.text_field :first_name, class: 'form-control', autofocus: true, placeholder: 'First Name'
For those using Rails(4.2) Internationalization (I18n):
Set the placeholder attribute to true:
f.text_field :attr, placeholder: true
and in your local file (ie. en.yml):
en:
helpers:
placeholder:
model_name:
attr: "some placeholder text"
I tried the solutions above and it looks like on rails 5.* the second agument by default is the value of the input form, what worked for me was:
text_field_tag :attr, "", placeholder: "placeholder text"
Here is a much cleaner syntax if using rails 4+
<%= f.text_field :attr, placeholder: "placeholder text" %>
So rails 4+ can now use this syntax instead of the hash syntax
In your view template, set a default value:
f.text_field :password, :value => "password"
In your Javascript (assuming jquery here):
$(document).ready(function() {
//add a handler to remove the text
});
This way works to me.
<%= form_for #product do |f| %>
<%= f.label :name %>
<%= f.text_field :name, placeholder: "Drill hammer" %>
<% end %>
As you can see, to implement a placeholder, you just can add the "placeholder: "text here", after your text_field name.
Hope my answer can be understood!

Resources