search through array in form_for rails - ruby-on-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

Related

How to put angularjs attributes in rails erb

I have a rails form that looks like this:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.text_field :email %></div>
<div><%= f.label :password %><br />
<%= f.text_field :password %></div>
<div><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off" %></div>
<div><%= f.submit "Update" %></div>
<% end %>
However, I'd like to tie the email text field to an AngularJS model, so that the value of the email form input is always tied to a $scope variable. In plain HTML, it would look like this:
<input name="email" ng-model="myValue"></input>
However since it's erb and not plain html, I'm not sure how to tie angular data to it.
How can I connect an ng-model attribute to an erb form in rails?
You should pass the angular attributes in the data attribute of the input, like so:
<%= f.text_field :email, data: { 'ng-model' => 'myValue' } %>
This will add the angular attribute with a data- prefix.

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"], ...]

f.select doesn't change model object

I have model called Polynom and model parametres are degree and type, i have form
<div class="field">
<%= f.label :degree %><br />
<%= f.number_field :degree %>
</div>
<div>
<%= f.label :type %><br />
<%= f.select :type, options_for_select(
[ "Laguerre Polynomials", "Legendre Polynomials" ]
)
%>
</div>
When i submit this form - degree changes, but type doesn't change. What i do wrong? degree is integer, and type is string values. Please help cause i dont know what to do
type is a reserved word - try renaming the attribute polytype or something ...

Multiple Checkboxes, Convert to String, Single DB Column Rails

I have a form, that among other things, contains about 20 different checkboxes. Like so:
<%= form_for #inventory do |f| %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
...
<p>
<%= f.check_box :apple %><%= f.label :apple %><br />
<%= f.check_box :banana %><%= f.label :banana %><br />
<%= f.check_box :orange %><%= f.label :orange %>
...
</p>
...
<% end %>
What I want to do is take the value of the selected checkbox, comma delimit them, and save them in a column in the db. So if the apple and orange checkbox is checked it saves as:
#inventory.fruit = "apple, orange"
how do I do this?
I don't think we can send multiple values as a string rather than an array. Look at the below solution
In Rails, how to handle multiple checked checkboxes, just split on the , or?
The solution is in pure HTML code but you can use check_box_tag instead.

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