set data attribute in grouped_collection_select dynamically in rails 4 - ruby-on-rails

I have a form like this in my orders/new page. I have used this to select model location and then choose a model from the location according to what is selected in the above select box.
<%= f.label "SELECT MODEL LOCATION *" %>
<div class="list_number">1</div><%= f.collection_select :location_id, Location.all, :id, :formatted_display, prompt: true, :required => true, :class => 'chosen-select' %>
<%= f.label "SELECT YOUR MODEL *" %>
<div class="list_number">2</div>
<%= f.grouped_collection_select :performer_id, Location.order(:name).map{|group| Performer.find_by_location_id(group.id).map{|performer| {'data-markup'=>performer.white_label.markup}}}, :performers, :name, :id, :first_name, include_blank: true, :required => true, class: 'chosen-select' %>
In the grouped_collection_select when I use map to set the data attribute I am getting the following error:
undefined method `map' for #<Performer:0x00000006813958>
How do I solve this error and get the data required?
After trying the below code
<%= f.grouped_collection_select :performer_id, Location.order(:name).map{|group| Performer.where(location_id: group.id).map{|performer| {'data-markup'=>performer.white_label.markup}}}, :performers, :name, :id, :first_name, include_blank: true, :required => true, class: 'chosen-select' %>
I get the following error
undefined method `performers' for [{"data-markup"=>#<BigDecimal:67e80c8,'0.0',9(36)>}]:Array
When I tried the following
<%= f.grouped_collection_select :performer_id, Location.order(:name), :performers, :name, :id, :first_name,Location.order(:name).map{|group| Performer.where(location_id: group.id).map{|performer| {'data-markup'=>performer.white_label.markup}}}, include_blank: true, :required => true, class: 'chosen-select' %>
I get the following error
undefined method `merge' for #<Array:0x000000069705a8>

Performer.find_by_location_id(group.id)
returns an instance but map is method for Array.
The fix depends on your business logic (and APP structure)
You can use Performer.where(location_id: group.id).map... if there are many Performers
I think you try to use this helper wrongly. According to documentation you should pass a collection and group_method. group_method - The name of a method which, when called on a member of collection, returns an array of child objects representing the <option> tags.
You pass an array of objects [{'data-markup' => some_value}, ...] as collection and performers as group_method. Rails try to call {'data-markup' => some_value}.performers and this raises an exception.
You should rewrite a collection the following way:
1. it should be array
2. each item of array should have method performers - which return an array of objects under one group item
I hope it will help you
I can't test but it can work (if Location has_many performers or you can implement your own method according to your business logic)
<%= f.grouped_collection_select :performer_id, Location.order(:name), :performers, :name, :id, :first_name, include_blank: true, :required => true, class: 'chosen-select' %>

Related

collection_select issue in rails with passing reference_id in rails

i am passing reference id as from requirement table in collection_select but instead of showing team_id as drop-down i want to user as team_name from team table by using team_id.
also i want distinct record.
form.html.erb
<div class="field columns large-4">
<%= form.label :team, :class=>"" %>
<%= form.collection_select :requirement_id, #project.requirements, :id, :team_id, prompt: true %>
</div>
Try following, Use delegate in model
In models/requirement.rb
delegate :name, to: :team, prefix: true
or use custom method
def team_name
team ? team.name : 'No team associated'
end
& then value_method in view
<%= form.collection_select :requirement_id, #project.requirements, :id, :team_name, prompt: true %>

Translating form_for syntax to simple_form syntax

I have this code within my rails form:
Categories: <%= f.collection_select :tag_ids, Tag.order(:name), :id, :name, {}, {multiple: true} %>
This code is working, but I want to use simpleform gem to redesign my form. However, I cannot seem to figure how to 'translate' this code into simple form. Anyone have any idea how? Thanks.
Something like this should do the trick:
If you have a many to many relation you could first try what the default does.
<%= f.association :tags %>
If the defaults don't work out you can make an explicit collection:
<%= f.input :tag_ids, as: :select, collection: Tag.order(:name), label_method: :name, input_html: {multiple: true} %>
# or
<%= f.input :tag_ids, as: :select, collection: Tag.order(:name).pluck(:name, :id), input_html: {multiple: true} %>
Alternatively if you define the Tag#to_label method you don't have to pass the name of the label method. The Tag#id gets used as default value method. If you would like another value specify the method like so: value_method: :something_else.
See the simple_form Usage section (intro, collections and associations).

Rails: Default Value - select field

I want to display a default select value. What I have so far is:
<%= f.collection_select(:user_id, User.where(brand: current_user.brand), :id, :name, {prompt:true}, {class: 'form-control'}, :selected => current_user.brand) %>
But this produces an error:
wrong number of arguments (7 for 4..6)
Any ideas what the problem is?
This should do the trick:
<%= f.collection_select(:user_id, User.where(brand: current_user.brand), :id, :name, {prompt: true}, {class: 'form-control', selected: current_user.brand}) %>
selected should be in the html_options. See also:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select

Use array from model in form drop down

Ruby on Rails 4
In my model I have:
#category_check = ['cables', 'printers', 'monitors', 'accessories', 'towers', 'interaction']
It is used for a validates, now I want to display the array as options in my form. This does not work:
<%= f.label :category %><br>
<%= f.collection_select :category, #category_check, {prompt: "Select Category"}, class: "input-lg" %>
Do I have to make another instance variable in my controller or is there a way to display each in a drop down from the model variable? Thank you
You need to call something like this
<%= f.collection_select :category, #category_check, :to_s, :to_s, {prompt: "Select Category"}, class: "input-lg" %>
This will pass :to_s to each element of the the #category_check collection.
You should be able to access that array through the model, assuming you have one instantiated:
#my_model.category_check
However it looks like those categories are static, so ideally they'd be a class-level constant:
class MyModel
CATEGORY_CHECK = ['cables', 'printers', 'monitors', 'accessories', 'towers', 'interaction']
end
Then you could access it anywhere, without an instance of the class around:
MyModel::CATEGORY_CHECK
As Lalitharyani mentions in his answer, you'll also need to provide name and value methods to call on each item of the array so that the form helper knows what to display.
You are probably missing one parameter on the method call:
<%= f.collection_select :category, #category_check, {prompt: "Select Category"}, class: "input-lg" %>
should be:
<%= f.collection_select :category, :category_id, #category_check, {prompt: "Select Category"}, class: "input-lg" %>

How to use a table column in my select box on rails?

I'm trying to create a select box that takes data from my db. I'm having trouble setting this up. I tried this code:
<%= f.fields_for :unit do |u| %>
<%= u.label :name %>
<%= u.select :name, :class => "ingredient_unit", :prompt => "Please Select" %>
<% end %>
but I'm missing the part of the choices, I don't know how to pull them out of the database. I tried using collection_select, which worked, but then the class option wasn't working... collection_select went like this:
<%= u.collection_select :unit, Unit.all, :id, :name, :class => "ingredient_unit", :prompt => "Please Select" %>
I also don't understand what the first symbol means (:unit), it seems to be setting the html id and name, so that can be anything I want it to be?
You should look at the documentation for collection_select and select. But to answer your question, for the select part, you forgot to pass the list of options to choose from. You also need to swap the order for prompt and class since prompt is an option for the helper and class is an html option
<%= u.select :unit_id, Unit.all.map { |u| [u.name, u.id] }, { :prompt => "Please Select" }, { :class => "ingredient_unit" } %>
For the collection select
<%= u.collection_select :unit_id, Unit.all, :id, :name, { :prompt => "Please Select" }, { :class => "ingredient_unit" } %>
The first parameter passed to both helper is the column name where you want the selected answer to be saved. The 2 codes above just shows 2 different ways to generate the same select tag.
The first symbol tells it which field to populate with the id returned from the user selection.
Also, you should wrap your class section in {}
:unit refers to the model attribute that you're using for the select element. Yes, it will setup the name/id of the element (and name is the most important for the params hash).
To set a class in the collection_select, specify it as a hash as that helper takes it as an html_option.
<%= u.collection_select :unit, Unit.all, :id, :name, { :prompt => "Please Select" }, { :class => "ingredient_unit" } %>

Resources