I'm fairly new to rails and I'm building my first app. I've searched the web for a correct answer but couldn't find any that worked for my case.
I'm using simple form, with rails 4 and bootstrap 3. I have a :location dropdown on a (#employees) model and I want to populate it with a job_title column from my #positions model. I've used a scaffold to generate my position MVC + job_title:string job_description:string.
How can I populate my :location dropdown (on my employees form) with values from :job_title (from #positions model)? I currently have my code as:
<div class="col-md-6 pad-10">
<% options = options_from_collection_for_select(#positions, 'id', 'job_title') %>
<%= f.select :location, options, :input_html => { class: "form-control" } %>
</div>
But as you know, that doesn't work. Any help is appreciated!
Solution:
<div class="col-md-6 pad-10">
<% options = options_from_collection_for_select(Position.all, 'id', 'job_title') %>
<%= f.select :location, options, :input_html => { class: "form-control" } %>
</div>
I used Position.all instead of #positions.
options_from_collection_for_select needs an Array. Docs
So try using Modelname.all instead of #positions or re-declare #positions with #positions = Modelname.all
Because you are using simple_form, you can render a select box for a collection like this:
<%= f.input :location, collection: #positions, label_method: :job_title, value_method: :id, input_html: { class: "form-control" } %>
This should do it for you if you want the id ofan Position object be written to the location field. If you want the job_title cahnge :id to :job_title.
<div class="col-md-6 pad-10">
<%= f.label :location %><br />
<%= f.collection_select :location, #positions, :id, :job_title %>
</div>
I used Position.all instead of #positions.
<div class="col-md-6 pad-10">
<% options = options_from_collection_for_select(Position.all, 'id', 'job_title') %>
<%= f.select :location, options, :input_html => { class: "form-control" } %>
</div>
Related
I have category names entered into a categories table in my db. I wanted to assign a product to that category via simple_form. I cant get it to pass through and accept the category.
This is my selector..
<%= simple_form_for :pack, url: packs_path do |f| %>
<div class="form-group">
<%= f.collection_select :category, Category.all, :id, :name,prompt: "Select Category", class: "form-control center" %>
</div>
<%= f.submit "Add product to grid", class: "btn btn-success btn-block" %>
<% end %>
This is my packs controller
def index
#packs = Pack.includes(:category).group_by { |pack| pack.category.name }
#categories = Category.all
end
My associations are as follows;
Category model: has_many :packs
Pack model: belongs_to :category
You can use SimpleForm shortcut to do this:
f.association :category, prompt: "Select Category", input_html: { class: "form-control center" }
Looks like you are using Bootstrap, you should use this command to integrate SimpleForm and Bootstrap: rails generate simple_form:install --bootstrap.
After doing this, you don't need to specify something like form-group, form-control, ....
In simple form, you have to use this.
<%= f.input :category, collection: Category.all, label_method: :name, value_method: :id,label: "Category", include_blank: false, selected: #pack.category_id %>
I can't seem to get this prompt text to show, despite it seemingly being constructed exactly as my others are. What am I overlooking? Thanks for any help!
This one doesn't work
<div class="form-group">
<%= form.label :role %>
<%= form.select :role, options_for_select([["University Staff", "staff"], ["Private Clinician", "clinician"]]), {prompt: 'Please Select Your Role'}, { class: "form-control" } %>
</div>
This one does
<div class="form-group">
<%= form.label :university %>
<%= form.select :university_id, options_for_select(University.all.map{ |uni| [uni.name, uni.id] }), {prompt: 'Please Select Your University'}, { class: "form-control" } %>
</div>
I was advised to create a user model class method for selecting the role, and this somehow allowed the prompt to work.
I am trying to use a collection_select for state just to filter cities, but I don't want to save the state_id in db. My "college" model has only city and college field. That's why it's throwing while instantiating :state_id I believe, I am new to rails. I am not able to figure it out. :(
<%= form_for #college do |f| %>
<div class="form-group">
<%= f.label "Select city"%>
<%= f.collection_select(:state_id, State.all, :id, :state, {}, {class: "form-control"})%>
</div><br>
<div class="form-group">
<%= f.label :city_id, "Select city"%>
<%= f.collection_select(:city_id, City.all, :id, :city, {}, {class: "form-control"})%>
</div><br>
<div class="form-group">
<%= f.label :college, "college Name"%>
<%= f.text_field :college, class: "form-control", placeholder: "Enter city name", required: true%>
<br>
</div>
<div class="form-group">
<%= f.submit class: "btn btn-primary"%>
</div>
<% end %>
just define state_id attribute accessor in your college.rb model :
attr_accessor :state_id
inside your model you should define state_id as attr_accessor like :-
class College
attr_accessor :state_id
end
I have two collection_select field in my form. One is to select category and the other is to select subcategory. I want the subcategory field to populate as per the category gets selected. I someone select category 1 then the subcategories belong to category 1 will be populated in the second collection_select field. I'm not sure what to do. What modifications I need in the following code I tried.
<div class="field">
<%= f.label :category_id %><br>
<%= f.collection_select(:category_id, Category.find(:all), :id, :name) %>
</div>
<div class="field">
<%= f.label :subcategory_id %><br>
<%= f.collection_select(:subcategory_id, Subcategory.where(category_id: :category_id), :id, :name) %>
</div>
You need to ajax to get this functionality
Try this Example:
form.html.erb
<script>
$(document).ready(fnction(){
$('#category_id').change(function(){
var value = $(this).val()
$.ajax({
type: 'POST',
url: 'options',
data: {id: value},
success: function(data){
$('#subcategory').html(data)
}
})
})
})
</script>
<div class="field">
<%= f.label :category_id %><br>
<%= f.collection_select(:category_id, Category.find(:all), :id, :name) %>
</div>
<div id="subcategory"></div>
controller:
def options
id = params[:id]
#subcategory = Subcategory.where(:category_id => id)
render :partial => 'options'
end
_options.html.erb:
<%= f.label :subcategory_id %><br>
change this line
<%= f.collection_select(:subcategory_id, #subcategory, :id, :name) %>
to
<%= f.input :subcategory_id, #subcategory %>
and finally in Routes.rb
post '/options' => 'categories#options'
You have two ways:
Use jquery. Something like that -
$(select_category).on('change', function(){ update_subcategory_options() })
where update_subcategory_options() - ajax request or another code.
Or use collection select with group (http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/option_groups_from_collection_for_select)
I have the following (and working) dynamic menu / dropdown which allows you to select a property type and then a property subtype with a regular rails form:
properties.js.coffee
jQuery ->
prop_sub_types = $('#property_prop_sub_type_id').html()
$('#property_prop_type_id').change ->
prop_type = $('#property_prop_type_id :selected').text()
escaped_prop_type = prop_type.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/#])/g, '\\$1')
options = $(prop_sub_types).filter("optgroup[label='#{escaped_prop_type}']").html()
if options
$('#property_prop_sub_type_id').html(options)
else
$('#property_prop_sub_type_id').empty()
_form.html.erb
<%= form_for(#property) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :prop_type_id, 'Property Type' %><br />
<%= f.collection_select :prop_type_id, PropType.order(:name), :id, :name, :prompt => "-- Select Property Type --" %>
</div>
<div class="field">
<%= f.label :prop_sub_type_id, 'Property Subtype' %><br />
<%= f.grouped_collection_select :prop_sub_type_id, PropType.order(:name), :prop_sub_types, :name, :id, :name, :prompt => "-- Select Property Subtype --" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
This works fine. However, I'd like to integrate this into a larger app that is already set up with the simple form gem. I'm also using twitter bootstrap via bootstrap-sass.
The closest I can get in my form is:
<div class="field">
<%= f.association :prop_type, :input_html => { :id => "prop_type_id", :class => "span5" }, :prompt => "-- Select Property Type --" %>
<%= f.association :prop_sub_type, :input_html => { :id => "prop_sub_type_id", :class => "span5" }, :prompt => "-- Select Property Subtype --" %>
</div>
Note: I had to change :prop_type_id to :prop_type to keep the app from throwing errors.
But this is not working - the second drop down won't map to the first. I a doing something wrong in my java/coffeescript? Is there such a thing as 'grouped_association' or something along those lines for the second dropdown?
Is this even doable, or should I convert the entire form back to the standard rails format?
UPDATE
I was able to get it to work but sticking the erb into divs as follows:
<div class="field">
<%= f.collection_select :prop_type_id, PropType.order(:name), :id, :name, :prompt => "-- Select Property Type --" %>
</div>
<div class="field">
<%= f.grouped_collection_select :prop_sub_type_id, PropType.order(:name), :prop_sub_types, :name, :id, :name, :prompt => "-- Select Property Subtype --" %>
</div>
You should have a look at the Group section in the simple_form_for github page. I was working on something similar to what you were doing and found this section. It gave me the direction that I needed to go. Instead of doing f.association which I used on other parts, I ended up using f.input with the :group_select and :group_method
f.input :country_id, collection: #continents, as: :grouped_select, group_method: :countries
from
simple_form_for github pag