In development environment my form is working correctly. But in production I find a city field (collection select) with a CSS attribute display: none;, which results in the collection select tag to not appear in my form.
<div class="field">
<%= f.label :city %>
<%= f.collection_select :city_id, current_user.country.cities, :id, :name, {prompt: "المدينة" } %>
</div>
Related
rails generate scaffold Post title:string body:text category_id:integer status_id:integer
generated the scaffold like this. category_id and status_id is used for check-box and radio button
Here the value are stored as a integer values like category 1 or 2 but i need to store the value of the category like tv or mobile in the database because we can not understand what product is required .
thank you in advance
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :body %>
<%= f.text_area :body %>
<%= f.label :category_id %>
<%= f.select :category_id, options_from_collection_for_select(Category.all, :id, :name, #post.category_id) %>
<%= f.label :status_id %>
<%= collection_radio_buttons(:post, :status_id, Status.all, :id, :name) %>
<%= f.submit %>
link is here i followed those steps
You shouldn't store the text value because it already in the Category table.
if you want to know what product is, you can use #product.category.try(:name) if associations setup correctly
I am trying to add a second dropdown menu to a form in my app. I have copied the code from the first drop down and changed the values to match the class I am trying to pull values from. When I remove the second drop down, the app runs smoothly, it is the second menu that returns an error.
uninitialized constant ActionView::CompiledTemplates::Providers
If i change 'Providers.order' to 'Provider.order' in my code, it returns this error:
undefined method `provider_id' for #<Bill:0x007fbf62544ee8>
Here is my code in the form:
<div class="field">
<!-- Drop Down menu for categories -->
<%= f.label :category_id %><br>
<%= f.collection_select :category_id, Category.order(:name), :id, :name%>
</div>
<div class="field">
<!-- Drop Down menu for providers -->
<%= f.label :provider_id %><br>
<%= f.collection_select :provider_id, Providers.order(:name), :id, :name%>
</div>
Try this:
<div class="field">
<!-- Drop Down menu for providers -->
<%= label :provider %><br>
<%= collection_select :provider, :provider_id, Provider.order(:name).all, :id, :name%>
</div>
I have a select_tag with options_for_select in a form_for to create an instance of a model but it's a huge list so I want to add a search (allow the user to type their selection and see the narrowed down results as they keep typing) instead of a select. Should I replace the select with a form_tag? How would I go about adding a search field within the form_for?
views/dogs/new:
<%= form_for [#master, #dog], role: "form" do |f| %>
<div class="form-group">
<div><%= f.label :name %><br />
<%= f.text_field :name, :autofocus => true, class: "form-control" %></div>
</div>
<div class="form-group">
<div><%= f.label :age %><br />
<%= f.number_field :age, class: "form-control" %></div>
</div>
<div class="form-group">
<div><%= f.label :breed %><br />
<%= select_tag "breed", options_for_select([['French Bulldog' ,'French Bulldog'], ['Pitbull', 'Pitbull']]) %>
</div>
currently appears as:
You would need to use a text field instead of a select field and use jQuery autocomplete(http://jqueryui.com/autocomplete/).
There's a nice example on how to use it.
You would need just to populate a variable in js with all the breeds in it and then use the plugin to filter the list and use one of its callback in case you need to customize your text field further; e.g. after a user has chosen the breed.
Hope this helps you
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>
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