Setting hint text in a text field in Ruby on Rails - ruby-on-rails

Can some one suggest the best way for setting hint text(not default text) for a text field in Ruby on Rails. Currently I am using this:
<%= text_field_with_auto_complete
"customer",
:contact_person, {
:value => 'last, first',
:style => 'color:#aaa;width:11em;',
:onfocus => "if(this.getValue()=='last, first'){this.clear();this.style.color = '#000';}",
:onblur => "if(this.getValue()==''){this.setValue('last, first');this.style.color = #aaa';}"
} %>
The text field is attached to a model and in turn to a value in a database. Also, the same html that is used in index.html is also used in edit.html and hence when I try to edit the field, the default value shows up instead of the value from the database. What is the correct way to do this?
Note: I am not trying to set a default value but just a hint to what needs to be entered in the text box.
Thanks,
Raja Ram

I'd recommend using the HTML 5 placeholder attribute, and then using a jQuery plugin to make it work on older browsers (like this one, but there are many others on Google).
You can see this technique used in production here.

Try this jquery plugin
http://plugins.jquery.com/project/hint
or choose here:
http://plugins.jquery.com/plugin-tags/hint
For prototype
http://davidchambersdesign.com/autopopulating-input-fields-with-prototype/

Related

Ruby on rails: Id attribute of input elements generated by form helpers, with non english values

Some Rails form-helpers (for instance 'radio_button' helper), append the element's value to the generated id string.
But if the value is not in English - nothing gets appended to the id string. This may result in multiple elements (all with non English values) having the same id attribute.
What's the recommended way to tackle this problem ?
This is a very old post, but it's coming up first in Google so I thought I'd spare some other rails developers a bit of misery by giving a code example of how to get this to work.
<%= radio_button_tag "name", "value", false, :class => "test-class", :id => "radio-id" %>
I just spent 20 mins trying to figure out how to do it and realised I was missing the 3rd parameter (checked). Aaargh!
Rails documentation here: https://apidock.com/rails/ActionView/Helpers/FormTagHelper/radio_button_tag
You can pass your own custom id in the options hash
http://apidock.com/rails/ActionView/Helpers/FormHelper/radio_button

simple_form: required vs. optional fields via AJAX

I have a form using simple_form, and trying to update some optional fields in it to required with AJAX.
In the Rails js.haml template, I am adding the class 'required' to the field label and input, e.g.:
$("label[for=some_label]").addClass('required')
$("input[id=some_input_id_here]").addClass('required')
but it does not work.
Has anyone done a similar thing with AJAX and simple form?
Another option is to re-render the form from the js.haml template, and to set :required => true for the necessary fields, however I think this is an overkill.
Ideas?
Thanks

How to display selected value from the drop down menu in Rails?

I am a Rails newbie. I have created a drop down using collection_select and grouped_collection_select. My issue is that I am not able to display the selected option value from the drop down list. I have to display only the selected value on a new page but I am not able to get it.
Use the :selected => params[:name_of_form][:name_of_field]
= f.select :category,
options_for_select(['option', 'value']... etc ),
:selected => params[:user][:category]
I cant remember if the symbol goes on the inside like above, or if it
You should really be more clear when you are asking questions on SO, and you should also include code.
ALWAYS ALWAYS ALWAYS INCLUDE CODE!

How do I align label for checkbox in formtastic for rails 3?

I am using the following for a check-box on a form:
= f.input :post_linkedin, :label => "Post to my LinkedIn Network",
:hint => "Sharing your expertise on LinkedIn helps you gain credibility, visibility and potential opportunities"
However, the rest of the form has labels aligned to the left of the fields. This puts the checkbox immediately to the right? Is that right?
The "hint" ends up being on the same line rather than as a separate line.
How can I make it appear on a separate line (without getting into css, wanted to see if formtastic could take care of it). But if I do, what would it be?
There's nothing you can do in Ruby, or with Formtstic - you're going to have to control it using CSS.

CKEditor and prefilling the textarea

I am using MVC.NET and CKEditor and want to prefill the textarea so on edit forms the text is already loaded.
I try with:
<%= Html.TextBox("Message", Model.Message) %>
I can see that the textarea contains the correct text, but since CKEditor alters the html, it isn't inserted into the editor. Is there a way to hack it so I can insert the text into the form?
It was a stupid error, a simple:<%= Html.TextAreaFor(model => model.Message) %> made it work.
CKEditor is desined for WebForms and its designed in such a way to depend on the external dependencies like request and response.
Unless the CKEditor company provides the helper extension for MVC you will not be able to do that. You can also search if there is any extension points provided like interfaces.

Resources