Cant add a second dropdown in my form - ruby-on-rails

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>

Related

Field with display:none in Rails form

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>

search through array in form_for rails

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

Can't seem to pass variable using select_tag

I have a form in Rails that I need to create an option tag in HTML. It's a pretty simple option list with static values. I could create it in HTML easy enough, but I want to do it the "Rails Way"
Here is a portion of my form:
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :last_name %><br />
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :phone %><br />
<%= f.text_field :phone %>
</div>
<div class="field">
<%= f.label :area %><br />
<%= select_tag "area", options_for_select([["Northeast", "NE"], ["Southeast", "SE"], ["Central", "CE"], ["West", "WE"], ]) %>
</div>
<div class="actions">
<%= f.submit %>
</div>
When I use the select_tag, my form shows that it is successfully posting changes, but the value that is passed by the select_tag is not updated to the database. I have verified that a simple text field DOES pass the value.
I'm sure I'm missing something very simple, but I'm having a difficult time identifying it. Please help. Thanks!
Rails Newbie
Check your rendering: a bare select_tag will render "area" as the name/id, whereas everything else in your form will have a normal model-based name.
For example, if the model was "user", the phone field's name would be "user[phone]".
You can see this happening by looking at your parameters passed to the action in the log.
Either use the form helper, or name it correctly the same as the other fields.
See also: select, select_tag, select (not helpful), and form helpers. The "Understanding Parameter Naming Conventions" section might also be of interest.
select_tag is like text_field_tag: it doesn't cooperate with the form builder (the object form_for yields) in order to get the proper parameter name and current value.
the select helper is ever so slightly different to select_tag in that it calls options for select for you, so you only need to write
f.select 'area', [["Notheast", "NE"], ...]

How to have predefined categories?

I want my users to be able to select from predefined values of categories. I took a look at other Stackoverflow questions but didn't get them fully. Here is my code right now....
I have my Categories model and Users create prices (which is another name for Items).
class Category < ActiveRecord::Base
has_many :prices
def name
"#{category}"
end
end
My prices belong to categories. note: The prices table has a category_id.
class Price < ActiveRecord::Base
attr_accessible :name, :date, :price
belongs_to :user
belongs_to :category
end
Here is how the form and view look as of now:
Form
<%= form_for(#price) do |f| %>
<div class="field">
<%= f.label :date %><br />
<%= f.date_select :date %>
</div>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :price %><br />
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :category %><br />
<%= f.collection_select :category_id, Category.all, :id, :name, :prompt => true %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
View
<p><b>User:</b><%= #price.user_id %></p>
<p><b>Date:</b><%= #price.date %> </p>
<p><b>Name:</b><%= #price.price_name %></p>
<p><b>Price:</b><%= number_to_currency(#price.price) %></p>
<p><b>Category:</b><%= #price.category.name %></p>
How do i create the categories i want in the drop down menu?
Thank you I'm a very appreciative!
You probably want the collection_select form helper method: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
It will look something like this:
<div class="category">
<%= f.label :category %><br />
<%= f.collection_select :category, Category.all, :id, :name %>
</div>
Then in the view:
<p><b>Category:</b><%= #price.category.name %></p>
This assumes that your categories table has a name field which stores the category names.
you can create your data in db/seeds.rb
rake db:seed to load them (Load the seed data from db/seeds.rb)
http://railscasts.com/episodes/179-seed-data for more details

Rails 3: add object using collection_select

I have the following code inside the _forms view.
<div class="field">
Shop: <%= f.collection_select :shop, #shop, :shopname, :shopname %><br />
</div>
However, no object is added to the database when I submit the form. This however works:
<div class="field">
<%= f.label :shop %><br />
<%= f.text_field :shop %>
</div>
This where I am adding manually the id. The collection_select does retrieve all the shopnames in the dropdown list.
Thank you for your help!
Maybe like that is better if shop is really a reference in your case
<div class="field">
Shop: <%= f.collection_select :shop_id, #shops, :id, :shopname %><br />
</div>

Resources