Set select rails with no related model collection - ruby-on-rails

Im trying to setup a combobox with a no related model collection, it will be displayed in my Stand model new view, but they are not related directly.
It is like show the classic country -> state -> city relation form where country and city are not directly related, so what I want to do is just show a "country" combo when I am creating a new city to display just related states! PLS HELP! IM DYING DOING THIS!
I want to do something like this:
<div class="form-group">
<%
concat f.cr_ubicacion.select :codpabellon,
Mtopabellon.order(:nombre), :codpabellon, :nombre, {},
{class: "store-select"}
%>
</div>

Solved it!
<%
concat f.select :idubicacion, Mtopabellon.all.collect {|p| [
p.nombre, p.codpabellon ] }, {prompt: 'Seleccione una
pabellon'}, :class=>'form-control', :id => 'cmbPab'
%>
Just setup the collection I needed even using a different :idubicacion and it works!

Related

Rails drop down menu not saving selected data

I am returning to rails and hobby programming after several years away. I started a new project and I have run into an issue using drop down menus. I am positive it is a small mistake but I am having trouble finding an answer online.
I am making a database of Restaurants. I have a model setup called "meals" which has seeded data for breakfast, lunch, dinner. When creating a new restaurant entry, I want to be able to select from a drop down which meal category the restaurant serves which then saves the meal_id to the restaurant entry. I have meal belongs_to restaurant and restaurant has_one meal. I copied and pasted the below code into the restaurant's form.html.erb and after it didn't work I found more examples online and I do believe it should have worked but it keeps showing that it never saved that data.
<div
class="field">
<%= form.label "Meal" %>
<%= form.collection_select(:meal_id, Meal.all, :id, :meal, { :prompt => 'Select a Meal', :selected => #restaurant.meal_id }, { class: 'form-control' }) %>
Thanks for the help!
One issue:
This is the method, note the order of the variables:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
So try this (if Restaurant is the object of the form):
form.collection_select(:restaurant, :meal_id, Meal.all.select(:name), :id, :name, { :prompt => 'Select a Meal' }, { class: 'form-control' })
Your real issue could be elsewhere, but without being able to see the whole form ERB or the controller code, I can only troubleshoot what I see.

Rails multiple drop down make it stick

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 }

Rails 3 Select Box helper, how to add custom options at the bottom that are static

How can I create a select box that has the following given 4 results for User.departments:
Department.title (Department.abbreviation)
Department.title (Department.abbreviation)
Department.title (Department.abbreviation)
Department.title (Department.abbreviation)
-----
Add New Department
Based on the following models:
User.department_id
Departments (id, title, abbreviation)
What I can't figure out is how add the two options at the bottom that say Add New Department.
Here is what I have so far:
<%= collection_select(:user, :department_id, Department.where(:id => current_user.department_id), :id, :title, {:prompt => true}) %>
Thanks
Your best bet is to use select instead of collection_select for this, here is an example of how I would do it.
<%= f.select(:user, Department.where(:id => current_user.department_id).collect {|p| [[p.title,' (', p.abbreviation,')'], p.id] } + ['Add New Department']) %>
Then you could use something like javascript to do something when 'Add New Department' got selected or however you planned on using it.
Hope this helps and happy coding.

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.

Basic rails collection select question

A card can have one of many card_types. There are two models, card and card_type, where card_type is an [id, card_type_desc] pairing.
When you define a new card, you have to pick a card-type from a drop down list.
I have the list rendering correctly with the below collection_select box, but the new card.card_type_id field is NULL. How do you set it to the value from the list?
<%= collection_select(:card_type, :id, #card_types, :id, :card_type_desc) %>
Thanks in advance.
I am guessing, It should be...
<%= collection_select(:card, :card_type_id, #card_types, :id, :card_type_desc) %>

Resources