My problem is that I have, for example, Product, Category and ProductCategory.
ProductCategory makes possible for a Product have several Categories
I would like to implement this using Select2 (http://ivaynberg.github.io/select2/) using the select2-rails gem (https://github.com/argerim/select2-rails)
I already know how to relate the models but I can't figure out how to implement the Select2 specific code.
EDIT:
Now I see that my problem was not much about select2, so I added this comment and changed the title hoping that it can help somebody else
Now I see that my problems were not about select2 but to do a multi select.
The code in the _form.html.erb that make it work is this one:
<%= f.label :category_id %>
<%= f.collection_select :category_ids, Category.order(:name), :id, :name, {:selected => #product.category_ids, :include_blank => true}, {:class => 'col-xs-12 col-md-7 padding_15', :multiple => true} %>
I also included :category_ids in the attr_accessible on models/product.rb
And the select2 specific, I included in a .js file
$(document).ready(function() {
$('#product_category_ids').select2();
});
I'm including these links as they helped me but pay attention on the differences depending on the Ruby/Rails versions
http://www.dzone.com/snippets/using-mutiple-collection
http://www.alethe.com/brad/2009/10/multiple-select-list-in-rails/
Just to let you know, unexpectedly, if this collection_select is the last line in my form, some form fields are disabled although there is nothing that states this in the source. Changing the order this problem doesn't exist.
I also don't know why the look is a little different than the other fields (I'm using Bootstrap 3)
Related
In Rails 4 in view I have
<%= form_for #person do |f| %>
<%= f.collection_select :country_id, Country.order(:name), :id, :name, include_blank: "Select your country" %>
...
<% end %>
I'd like "Select your country" to be selected as default whenever the page is loaded. One way is to use javascript (select it after the dom is loaded). Is there an easier way like adding an option to collection_select?
Thanks.
As per the docs, it's the prompt option in the options argument:
collection_select(:post, :author_id, Author.find(:all),
:id, :name_with_initial,
{:prompt => 'Please select the author of this post'})
With collection_select on a form builder we omit the first argument, so in this case:
f.collection_select :country_id, Country.order(:name), :id, :name, {prompt: 'Select your country'}
I've 100% confirmed this as working on my own app running Rails 4.1.6, where prompt and include_blank do the same thing.
The way this works is Rails injects a null-valued <option> as the first item in the generated <select> (this is because the HTML spec has nothing analogous to placeholder on text inputs for select inputs).
Reasons this may fail:
Rails does not mark the prompt option with the selected attribute, and I suspect some browsers may choose to render their own blank entry instead of the first in the list
If, for existing records, Rails determines that the current record's country_id matches an element in the list it will mark that one as selected. This is expected behaviour but can be a pain if you're doing anything non-standard.
If you're being bitten by these problems your options are to build the form manually (the method options_from_collection_for_select may be of use here) or do it in javascript. There is also an undocumented default attribute you can add to an <option> tag but it's not in the spec and browser support may be patchy, and you'd still have to build the form manually.
I'm new in web development & Rails. I've been struggling to understand why my form is not getting saved completely. Here is the code I'm using:
<div class="field">
<%= f.label :type %><br>
<%= select_tag(:type, options_for_select([['Verb', 'Verb'], ['Adjective', 'Adjective'], ['Noun','Noun'],['Preposition','Preposition'],['Article','Article'],['Adverb','Adverb']])) %>
</div>
<div class="field">
<%= f.label :category %><br>
<%= select_tag "category",
"<option>Appliances</option>
<option>Clothes and Accessories</option>
<option>Colours</option>
<option>Communication and Technology</option>
<option>Documents and Texts</option>
<option>Education</option>
<option>Entertainment and Media</option>
<option>Family and Friends</option>
<option>Food and Drink</option>
<option>Health, Medicine and Exercise</option>
<option>Hobbies and Leisure</option>
<option>House and Home</option>
<option>Measurements</option>
<option>Personal Feelings, Opinions and Experiences (adjectives)</option>
<option>Places: Buildings</option>
<option>Places: Countryside</option>
<option>Places: Town and City</option>
<option>Services</option>
<option>Shopping</option>
<option>Sport</option>
<option>The Natural World</option>
<option>Time</option>
<option>Travel and Transport</option>
<option>Weather</option>
<option>Work and Jobs</option>".html_safe %>
</div>
PS: I've kept two different methods I tried to use.
use f.select instead of select_tag.
f.select(:type, [['Verb', 'Verb'], ['Adjective', 'Adjective'], ['Noun','Noun'],['Preposition','Preposition'],['Article','Article'],['Adverb','Adverb'])
or if you are using form_for and passing an object then you can also do it as follows.
select_tag(:type, options_for_select([['Verb', 'Verb'], ['Adjective', 'Adjective'], ['Noun','Noun'],['Preposition','Preposition'],['Article','Article'],['Adverb','Adverb']],f.object.type))
we are passing a value of type from actual object into option for select.
As you are using option_for_select it expects that you send selected value as a second parameter.
options_for_select also takes second parameter which is the selected value.
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select
# this will show Preposition selected
options_for_select([['Verb', 'Verb'], ['Adjective', 'Adjective'], ['Noun','Noun'],['Preposition','Preposition'],['Article','Article'],['Adverb','Adverb']], 'Preposition')
For future reference, please always specify Rails version while posting question.
I noticed you are using f.label, in which case you might also want to take a look at http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select
HTH
For the first one, you should use f.select instead of select_tag, rails tags are helpers for generate html elements, but in this case you need an item that is linked to your form, so you use the form helper for select instead.
For the other example, i'm not entearly sure if it will work like that, but try with the same idea, you should found that the select is passed to your controllers, also use the symbol name instead the string name, meaning :category instead "category", if you want to have a phrase like "select a category...." add another option at the end with :prompt => "select a category...", hope it helps and look at Ryan Bates site, is an excellent place to learn rails
I have a form and a group of checkboxes. But I want the user to be able to only check ONE value, never more than one.
This is what I have and it works for more than one value; it's on a has many through association. But now the clients wants to check only ONE, not more than one. I don't want to make changes to the DB or the associations because this client might change her mind later.
<%= f.collection_check_boxes(:community_partner_organization_ids, CommunityPartnerOrganization.order('name'), :id, :name, {}, {:class => 'checkbox'}) {|input| input.label(:class => 'checkbox') { input.check_box + input.text }} %>
Radio buttons, by nature, only permit the selecting of a single element, and ActionView provides a helper just like collection_check_boxes!
Give something like this a try:
<%= f.collection_radio_buttons(:community_partner_organization_ids, CommunityParnerOrganization.order('name'), :id, :name) %>
So I was experiencing an error when I was attaching a collection_select to my form_for object like so:
<%= f.collection_select(:city_id, #cities, :id, :name, {:prompt => "Select a City"}, {:id => 'cities_select', multiple: true}) %>
and getting the error:
undefined local variable 'city_id'
But when I don't bind the select like so:
<%= collection_select(nil, :city_id, #cities, :id, :name, {:prompt => "Select a City"}, {:id => 'cities_select', multiple: true}) %>
It works fine.
I just want to understand the theory behind why one works and the other doesn't?
I think what's tripping you up, primarily, is the concepts you have of what's going on here.
Nothing is “binding” anything to anything by calling a method on a form helper object. There are form helper methods, like collection_select, that can be used to build HTML elements. There are form builders that have methods, like collection_select that build HTML form elements for a form tied to an object.
The issue you're having here is that the FormOptionsHelper#collection_select method and the FormBuilder#collection_select method are not the same method and do not accept the same arguments:
FormOptionsHelper#collection_select vs FormBuilder#collection_select
Pay particular attention to the arguments provided. It's also worth noticing that FormBuilder essentially delegates this work to the template (i.e. FormOptionsHelper) and adjusts the arguments as needed.
In my app, there are two models: rfq and standard. Their relationship is many-to-many. In rfq creating screen, the code below displays a list of available for selection in drop down list:
<%= simple_form_for #rfq do |f| %>
<%= f.association :standards, :collection => Standard.active_std.all(:order => 'name'), :label_method => :name, :value_method => :id %>
<% end %>
The problem is that the list is not collapsed, which means there are multiple standards displayed in a multi-line boxes. How can I reduce the box to one line only?
Thanks.
UPDATED: here is the screen shot of multiple line list box:
It's creating a multi-select because one rfq can have many standards, so it allows you to ctrl-click to select many standards.
You could try adding :input_html => { :size =>'1' } but I'm not sure that will preserve the scrollbar. It definitely won't drop down.
Here's someone else who wanted to do the same thing: HTML muliple select should look like HTML select. One of the answers refers to a Dropdown Check List implemented in jQuery, but that would take some work to integrate with SimpleForm.
SimpleForm has a very helpful Google Group--you might get more ideas there:
http://groups.google.com/group/plataformatec-simpleform
You can add as: :collection_select
Use
=f.collecion_select, model_associated_ids, collection, value, label
in your is like this
=f.collection_select, :standard_ids, Standard.active_std.all, :id, :name
you can find more info here
https://github.com/plataformatec/simple_form