I am trying to output checkbox with hard coded checkbox value, so far I have coded
<%= form.collection_check_boxes(:study_type,['Option1','Option2'], :first, :first)%>
The output for checkbox label is first alphabet of respective checkbox. Any way to display full text value label.
Thanks for help.
You have two ways for this:
First, convert your array into an array of arrays like this:
<%= form.collection_check_boxes(:study_type, [['Option1'],['Option2']], :first, :first)%>
Or, you can use the :to_s method like this:
<%= form.collection_check_boxes(:study_type, ['Option1','Option2'], :to_s, :to_s)%>
Both solutions are "hacky" and I personally wouldn't use it like that. A collection of objects is what's usually used in collection_check_boxes. More info here.
Related
I am having a field named price_in_cents for materials table.
I want to display all the values of price_in_cents as dollars in drop down.
Here is the code:
<%= select_tag 'price_in_cents', "<option value='blank'>(empty)</option>"+options_for_select(Material.pluck(:price_in_cents).compact.uniq),include_blank: true, class: 'form-control' %>
Actually price_in_dollars = self.price_in_cents/(100*100).to_d
For example price 10200 has to be displayed as 1.02.
How to write or mention the values as price_in_cents/(100*100).to_d in drop down?
You can try like this:
<%= select_tag 'price_in_cents', "<option value='blank'>(empty)</option>"+options_for_select(Material.pluck('price_in_cents/(100*100)').compact.uniq),include_blank: true, class: 'form-control' %>
Here, performing operation while fetching record value. If you want to convert it into integer then you can write it as:
Material.pluck('CAST(price_in_cents/(100.0*100.0) as decimal)').compact.uniq
I have given example of postgresql
You probably want to keep this conversion logic out of your views-- this sounds like a situation where the Decorator Pattern would be appropriate and would recommend looking into a gem like Draper. You could add a price_in_dollars method to your MaterialsDecorator and use a decorated #materials in your view.
Or if you are strictly looking for an array of these values, a class method such as Material.prices_in_dollars might be appropriate.
If you are dealing with money, I suggest you use something like money-rails, it will add the necessary readers and writers for you to access the money value in dollars and save you some rounding headaches.
I have some problems with Simple_form and acts-as-taggable-on.
I have a object "Pictogram" with some tags like "door" and "emergency"
I use this code in my form
= f.input :tag_list
When I edit my Pictogram the system delete my comma and display my text_field like that "door emergency" if i save my two tags are deleted and replaced by one tag "door emergency"
I'm looking for a solution on the net and many peoples advise to create a custom input in simple_form but I don't where to begin.
I create a file tag_input.rb in app/inputs and I add this code
class TagInput < SimpleForm::Inputs::Base
def input(wrapper_options)
end
end
But now I'm lost.
Sorry for my English, it's not my native language
Here's what you do based on this
= f.input :tag_list, input_html: {value: #something.tag_list.to_s}
Custom input contains simple form tags
Check out this wiki https://github.com/plataformatec/simple_form/wiki/Adding-custom-input-components
Inside you can build comma separated string of values
value = object.tags.join(',')
text_field_tag attribute_name, value
Something like that, don't have a simple form nearby, so can't debug this code.
But the main idea I think is clear
I have a form with a pull down list of Races (asian, caucasian, African/black, etc).
These races are represented by my Race model. I can create a record successfully, however, when I try to view the record the corresponding integer of the race is displayed and not the text.
How do I get the text associated instead of the integer id?
Thanks.
Provided you supply a minimum set of data for us to assist, I risk an answer as generically as I can.
If you are using a form_for builder
<%= f.collection_select :race_id, Race.all, :id, :name %>
or whatever you call your fields
If you are using a form_tag
<%= select_tag :race_id, options_from_collection_for_select(Race.all, "id", "name") %>
These are as I said pretty generic answers, you can build further on them. Check this link for more:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html
You are seeing the value of the selected option which is the index of the array I imagine. You can use that index to get the text value from your races array. Without seeing more code that is all I can provide.
i tried this here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html
exactly this:
<%= collection_select(:sportlers, :sportlers_id, Sportler.all, :id, :name) %>
But there dont is written, how to get the selected value from the collection_select.
Im helpless, do you work with somethings like this?
I think collection_select will take the first two parameters, the object and the method, and use the values to make a hash, so in your case, you can access the values in your controllers like:
params[:sportlers][:sportlers_id]
I'm sure my title was hugely unhelpeful, I'll try and describe my problem better here. Essentially, I have a bunch of form tags, actually I'm using formtastic, so they look kinda like this:
<%= f.inputs :email, :pass, :passconf, :for => :register_attributes %>
These generate a group of input fields and labels with rather long id names, e.g. one such field is called 'posting_register_attributes_email'.
Now, I'd like to create a link that hides/shows these elements on request. It's easy to do brute force, e.g.:
<%= link_to_function "Register", "$('#posting_register_attributes_email').hide()" %>
But I feel surely there must be a more elegant way of doing it than typing out the full generated name, is there a helper that will let me doing something like [:register_attributes][:email].hide or something to make this less tedious? I feel like there must be.
Thanks!
$("[id*='register_attributes']").hide();