Changing ruby generated select to checkboxes - ruby-on-rails

Im very much new to ruby, but working on a new clients project and im wanting to change the current dropdown they have into multiple checkboxes but not sure how or if possible.
Current code is:
<%= f.label :section,t(:pick_admin_options)%><br/>
<% options = (#admin_permission.action == :edit) ? section_options('edit') : section_options %>
<%= f.select :section, options_for_select(options,:selected => #admin_permission.section) %>
Anyone have any ideas? much appreciated in advance!
Thanks

I think, This should do the trick:
<%= f.input :sections, :as => :check_boxes, :collection =>
((#admin_permission.action == :edit) ? section_options('edit') : section_options) %>
Thanks

Related

f.collection_select not displaying the selected value

There are so many results for this search on google, and it's even asked at SO - but the solutions discussed so far are not helping me. Here's the issue: I have a form_for #company |f| and I am using f.collection_select for company_status_id - but when the form loads, I want the actual company status selected if it is set. Through the debugger I know, that it's been set, yet I am getting a default value displayed there. Here's the code:
= puts #company.company_status_id
= f.collection_select :company_status_id, ListCache.company_statuses, :id, :name, {:prompt => #select_value}
Here's the generated htmnl
<select id="company_company_status_id" prompt="-Select-" name="company[company_status_id]">
<option value="1">-Not Available-</option>
<option value="2">Active</option>
<option value="3">Bankrupt</option>
<option value="4">Acquired</option>
</select>
And the conditions remain the same even if I do:
f.collection_select :company_status_id, ListCache.company_statuses, :id, :name, {:prompt => #select_value, :selected => :selected => #company.company_status}
Or
f.collection_select :company_status_id, ListCache.company_statuses, :id, :name, {:prompt => #select_value, :selected => #company.company_status}
This is what I finally did:
f.collection_select :company_status_id, ListCache.company_statuses, :id, :name, {:prompt => #select_value, :selected => #company.company_status_id.to_i}
I read on of the answers on a similar question that collection_select automatically selects the selected value by making comparisons of what is passed with the attributes of collection. apparently there was a difference of their types, and comparing the int from CompanyStatus to the int of #company.company_status_id.to_i worked out. Though #company.company_status_id is supposed to be int as well. I can see that in the db. Anyway, it this line of code worked.
If anyone can exaplain, I will be much thankful!
If you use collection_select helper, syntax is very simple:
<%= f.collection_select :category_id, Category.all, :id, :name,
prompt: true, selected: #product.category_id %>
I hope this help
<% form_for(#company) do |f| %>
<%= f.select(:company_status_id, ListCache.all.map {|lc| [lc.name, lc.id]} ) %>
<% end %>
Sometimes you just need to go to the browser address bar and press enter. Normal reloading the page clicking the refresh button doesn't help. My problem was solved that way.
Use select_tag instead
<%= form_for(#product, :html => {:multipart => true}) do |f| %>
<%= select_tag("product[category_id]", options_for_select(#categories.map { |cat| [cat.name, cat.id] })) %>
<%end%>
Hope this help you.....

Formtastic Gems input check_boxes didn't support checked/selected list

Am using formtastics in Active Admin gem Everything is working find except one thing that am passing it list to be checked/selected on check boxes list but not working.. So kindly help me in this regard. Thanks..
I tried this in Simple form Its working fine over there but not in Formtastic.
<%= f.input :surgery_type_procedures, :label=>false,:as=>:check_boxes,:required=>false,:collection => st.surgery_type_procedures.all.collect!{ |p| [p.to_s, p.id] },:checked=>#surgery_type_procedures %>
<%= f.input :surgery_type_procedures, :label=>false,:as=>:check_boxes,:required=>false,:collection => st.surgery_type_procedures.all.collect!{ |p| [p.to_s, p.id] },:selected=>#surgery_type_procedures %>
Didn't Select Any check_box out which are in "#surgery_type_procedures" so kindly tell me is formtastic support checked or uncheck option??/
<%= f.input :column_name, :input_html => {:checked => 'checked'} %>
Change p.id to p.id.to_s and remove checked/selected key. It should be works.

form_for wrong number of arguments (3 for 2) since upgrade to rails 3.1

this form_for used to work before I ported my application to rails 3.1
<div class="form-box" style="padding-left:1em;">
<%
action = #existing_mass.nil? ? "add_to_power_plant": "update_power_plant_substrate";
submit_button_label = #existing_mass.nil? ? 'Add': 'Update';
%>
<%= form_for :substrate_mass, #substrate_mass, :remote => true, :url => { :action => action, :substrate_id => #substrate_mass.substrate } do |f| %>
<div>
<%= f.label :quantity_per_year, "Quantity" %>
<%= f.text_field :quantity_per_year, :size => 5, :onclick => 'this.select();', :value => #substrate_mass.quantity_per_year %>
</div>
<div class="actions" style="float:right;">
<%= f.submit submit_button_label %>
</div>
<br/>
<% end %>
</div>
I have spent over 4 hours trying to figure out what's wrong ... there is definitely something I am not understanding anymore
I get the error:
wrong number of arguments (3 for 2)
Note that I am trying to update an variable that is not an activerecord object. It's just an object that is not stored in the database.
Hope someone can help.
cheers
form_for only takes two arguments, the record, and options, although record may be several things, including a simple symbol, an object, or an array.
Try just dropping the first symbol and sending your object. If you model does not include ActiveModel::Naming, you may set the name via the :as option.
<%= form_for #substrate_mass, :as => 'substrate_mass', ... %>
More help may be found here:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for
Or to view the source directly:
https://github.com/rails/rails/blob/v3.1.0/actionpack/lib/action_view/helpers/form_helper.rb#L353

Rails/Ruby delete_if

I have a select tag nested in my form and I am needing to delete an item from my options_for_select array if it equals the word English
code:
<%= fields_for :users_languages do |u| %>
<div class="field">
<%= u.label :Assign_Languages %><br />
<%= select_tag :language_id,
options_for_select(Language.all.collect {|lang|
[lang.english, lang.id].delete_if {lang.english == "English"}
}, #lang_list),
:multiple => true,
:prompt => 'Select Language' %>
</div>
<% end %>
Problem:
The above code works fine, but for some reason the first option is still shown in the mutli-select producing a blank select option. Is there anyway to get rid of the select option and its value? Am I even doing this correctly?
Thanks for all the help in advance!
You can do it using a collect and a reject.
Language.all.collect { |lang| [lang.english, lang.id] }.reject { |(lang, id)| lang == 'English' }
Not sure how to do it using just collect.
I realize this is a year old, but for the sake of future googlers...
Rather than inlining SQL, you might be better off with a named scope:
scope :not_english, where("english != 'English'") # Rails 3
or
named_scope :not_english, :conditions => "english != 'English'" # Rails 2
which will let you do:
options_for_select(Language.not_english.collect {|lang| [lang.english, lang.id]})
That is easier to read, more expressive, and not depending on some magic database id.
Cheers!
(PS: Also, you may want to consider updating your schema so the name of the db column isn't confused with it's value. May I suggest language.name, rather than language.english?)
It looks like your selection is actually returning an empty array as the first element. Try pulling out the unwanted option first:
Language.all.reject {|lang| lang.english == "English}.collect {|lang| [lang.english, lang.id]}
Thanks for the reply guys! What I ended up doing was handle it in my query
code:
<%= fields_for :users_languages do |u| %>
<div class="field">
<%= u.label :Assign_Languages %><br />
<%= select_tag :language_id, options_for_select(Language.find(:all, :conditions => "id != 1").collect {|lang| [lang.english, lang.id]}, #lang_list),:multiple => true, :prompt => 'Select Language' %>
</div>
<% end %>
the first row in the database will always be the same so this was easy to do in the query... although the suggestions above will come in handy in the future! Thanks again!

Forms blank when rendering a partial when using a collection of objects. Help!

Alright, I know my title is a little obscure but it best describes the problem I am having.
Essentially, I have a list of users, and want to be able to edit their information in-line using AJAX.
Since the users are showing up in rows, I am using a partial to render the data and the forms (which will be hidden initially by the ajax), however, when the rows are rendered currently only the last item has it's form's fields populated.
I suspect this has something to do with the fact that all the form fields have the same id's and it is confusing the DOM. But I don't know how to make sure the id's are unique.
Here is a small example:
In my view:
<%= render :partial => 'shared/user', :collection => #users %>
My partial (broke down to just the form) note that I am using the local variable "user"
<% form_for user, :html => {:multipart => true} do |f| -%>
<%= f.label :name, "Name*" %>
<%= f.text_field :title, :class => "input" %>
<%= f.label :Address, "Address" %>
<%= f.text_field :address, :class => "input" %>
<%= f.label :description, "Description*" %>
<%= f.text_area :description, :class => "input" %>
<% end -%>
When the html is rendered each form has a unique id (for the id of the user) but the elements themselves all have the same id, and only the last user form is actually getting populated with values.
Does anyone have any ideas?? :)
Thanks in advance!
Alright, after having some lunch and regaining some brain cells, (and with a little help from Google) I figured this one out.
When passing a collection to a partial like this:
<%= render :partial => 'shared/user', :collection => #users %>
Rails creates a counter variable that you can use to define an index for the form in the form of "variable_counter":
<% form_for user, :index => user_counter, :html => {:multipart => true} do |f| -%>
This adds the index number to the form id as well as all the field id's and solved my little problem. :)
I hope this helps out someone else with this issue. :)

Resources