Rails multiple drop down make it stick - ruby-on-rails

I have created dropdown language selection list, however I would like to make it stick when user edits. I know that it can be done with the second argument of select_tag but could not do it.
I have a user model and associated language model;
<%= f.label :language, "Spoken Languages" %>
<br>
<%= select_tag("user[language_ids][]", options_for_select(Language.all.collect { |ff| [ff.name, ff.id] }, #user.languages.all.collect { |kk| [kk.name, kk.id] }),
{:multiple=>true, :class => "language_select form-control"}) %>
EDIT:
Here how it looks like, even though spoken languages are set before;
But it should look like with the pre selected languages (comes from database);
I used Select2 to create the dropdown list.

Try changing this part to: #user.languages.collect { |kk| kk.id }

Related

TWO dynamic dropdowns with ONE model

I have a search field where I want to add two dropdowns that should help me filter my search results. In my database I have one Product model with two columns, named country and type.
The first dropdown should filter by country and the second should filter by type. However, I am trying to set the second dropdown dynamically meaning that depending on the first dropdown (country) only those types should be displayed in the second dropdown that match the respective country selected. If possible this behavior should also work the other way around i.e. depending on the type, only matching countries should be displayed.
I've been trying several tutorials that work with more than one model however, I didn't manage to get it to work with only one model. My code for the dropdown selections is the following:
<%= form_tag("filter", :id => "filter_form", :method => "post") do %>
<label for="country" class="country">Country</label> <%= select_tag :country, options_for_select(Product.pluck(:country).uniq), { include_blank: 'Select country' }%>
<label for="type" class="type">Type</label>
<%= select_tag :type, options_for_select(Product.pluck(:type).uniq),{ include_blank: "Alle Kategorien" }%>
Is is possible with only one model?
Do I need AJAX for this or is Javascrip/jQuery sufficient?
Thank you very much!

select_tag doesn't save the selected option

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

How to implement multi select in a independent table in Rails?

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)

Rails, Formtastic: checkboxes, but just allow to check ONE

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

Rails 3.1 HABT Model & Drop-Down Menu in Views

I've two models in my app that are joined together using a many -to-many association (both "has an belongs to" other model). Browsing on internet I've seen that there's thousands of examples that shows how set up view and controller, but all of these use checkbox_tag inside view, while I need a dropdown menu in my view, since objects that I've to display are more than 100, so you understand why I cannot use checkbox.
Have you experienced the same problem? In witch way do you have build view?
What you'd want to do is use a multi-select, so instead of a checkbox like so:
<%= check_box_tag "product[category_ids][]", category.id, #product.categories.include(category) %>
You'd want to do
<%= select_tag "product[category_ids][]", options_from_collection_for_select(#categories, "id", "name"), :multiple => true %>
The :multiple => true is the important part to convert it to a multi-select box.

Resources