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 ...
Related
I am trying to figure out the cleanest way to create multiple DB entries at once (or if it's even a good idea).
I have an Event model, which has many Bands and many Venues, through the event_bands and event_venues models.
Right now, the user first creates the event, then is redirected to the form to create the event_band and event_venue relationships. I would like to streamline all of this into one form if possible, like this (bottom lines are most relevant):
<%= form_for #event do |f| %>
<%= f.label :name %>
<%= f.text_field :name %><br />
<%= f.label :contact %>
<%= f.text_field :contact %><br />
<%= f.label :price %>
<%= f.text_field :price %><br />
<%= f.label :info %>
<%= f.text_area :info %><br />
<%= f.label :date %>
<%= f.date_select :date, start_year: 2014 %><br />
<%= f.label :time %>
<%= f.text_field :time, placeholder: "7:40pm" %><br />
<%= f.label :band %>
<%= select(:event_band, :band_id, options_for_select(Band.order(name: :asc).collect { |b| [b.name, b.id]} )) %><br />
<%= f.label :venue %>
<%= select(:event_venue, :venue_id, options_for_select(Venue.order(name: :asc).collect { |v| [v.name, v.id]} )) %><br />
<%= f.submit %>
<% end %>
Because the event_band and event_model require event.id, I guess I would have to use an after_create callback to build and save them. The issue I'm having conceptualizing this is that I want to post those extra :band_id and :venue_id params through the form for the event.
Is there a cleaner way to handle posting params for multiple models through one form and then using them in a callback or am I headed in the wrong direction?
You can use fields_for : see doc
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 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"], ...]
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.
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>