f.select not defaulting to correct value - ruby-on-rails

I have a data model that contains the fields for startMonth and endMonth. I also am able to SET them correctly using the following f.select tags, however, when I go to edit the data, the rendered dropdowns do not reflect the proper values stored in the db. Am i setting the f.select tags incorrectly?
<div class="control-group">
<%= f.label "Start Month", :class => 'control-label' %>
<div class="controls">
<%= f.select :startMonth, options_for_select([['January' ,'1'],['February' ,'2'], ['March', '3'],['April' ,'4'], ['May', '5'],['June' ,'6'],['July' ,'7'], ['August', '8'],['September' ,'9'], ['October', '10'],['November' ,'11'], ['December', '12']]), class: "list-select" %>
</div>
<%= f.label "End Month", :class => 'control-label' %>
<div class="controls">
<%= f.select :endMonth, options_for_select([['January' ,'1'],['February' ,'2'], ['March', '3'],['April' ,'4'], ['May', '5'],['June' ,'6'],['July' ,'7'], ['August', '8'],['September' ,'9'], ['October', '10'],['November' ,'11'], ['December', '12']]), class: "list-select" %>
</div>
</div>
Once the data is stored using these dropdowns, it stores it correctly in the database and it shows the correct values on my show page. However, I want the dropdowns to default to the stored values when I return to the edit page. Is there a setting I am forgetting to include?
Alex

You can pass a second argument to the options_for_select method to pre-select the value. For example, assuming your model object is represented by an instance variable called #object, you can pass in #object.endMonth as the second parameter to your options_for_select.:
<%= f.select :endMonth, options_for_select([['January' ,'1'],['February' ,'2'], ['March', '3'],['April' ,'4'], ['May', '5'],['June' ,'6'],['July' ,'7'], ['August', '8'],['September' ,'9'], ['October', '10'],['November' ,'11'], ['December', '12']], #object.endMonth), class: "list-select" %>
Note: the #object.endMonth value must resolve to the same data type as the option value - in this case the number representing the month. See the Rails Guide for details.

Related

Rails text_field_tag values are not getting properly

Hi in Rails how to display text_field values. In my index file I am trying display my values in text_field_tag but here is i am getting values in this format
considering bellow code
1) {:value=>2}
2) {:value=>0.3e2}
But I just want to display in text_field_tag values as
considering bellow code
1) 2
2) 300
How should i reformat it?
is there any other text_field(I don't want to use it in form field this is just index file for display values I don't want to submit it)
<div class="col-xs-4 col-sm-3"><%= text_field_tag :amount, value: 2 %></div>
//or
<div class="col-xs-4 col-sm-3"><%= text_field_tag :amount, value: expense.amount %></div>
Thanks a lot for your valuable answer :)
As mentioned here, text_field_tag accepts the second argument as the value to field. So, pass the value directly (instead of passing it as a hash):
<%= text_field_tag :amount, 2 %>
Because you are getting hash value as {:value=>2}
also you can convert it to integer
expense[:value].to_i
2.3.4 :003 > 0.3e2.to_i
=> 30
<%= text_field_tag :amount, expense[:value].to_i, class: "your_class", placeholder: 'some placeholder' %>
In case if you want static value
<%= text_field_tag :amount, 2, class: "your_class", placeholder: 'some placeholder' %>
In case if you want to put it in form but not to be submitted make is disable
<%= text_field_tag :amount, expense[:value].to_i, disabled: true, class: "your_class", placeholder: 'some placeholder' %>
Note: disabled field are not to be subjected to submit with form data, however :readonly => true will be wrapped with form datas
in other case just put this field outside the form

Serialized fields_for not populating from db on edit

On the edit page for this form all of the fields outside of the fields_for tag (inbox name, automatic reconciliation, and a few others not listed here) are all populating based on their corresponding db value. However, everything inside the fields_for tag are not, even though they're posting to the db just fine.
I posted :group_member_roles as an example but there are a few other fields inside their own other fields_for that are doing the same thing. It's just confusing that it will post to the db but not display on edit.
The more I read into fields_for the more I feel like I'm not using it correctly. It seems to be more inclined to populating db tables outside of the one your form is currently referencing, but I'm just trying to serialize data within the inbox table. When I look at the :group_member_roles column I want it to be an array/hash containing process true/false, action add/delete, and a string of values.
#_form.html.erb
<%= form_for(#inbox) do |f| %>
<%= f.label :inbox_name %>
<%= f.text_field :name, placeholder: "Inbox Name" %>
<%= f.label :automatic_reconciliation, "Turn on/off automatic reconciliation" %>
<div class="switch small">
<%= f.check_box :automatic_reconciliation, class: "switch-input" %>
<label class="switch-paddle" for="inbox_automatic_reconciliation">
<span class="show-for-sr">Automatic reconciliation</span>
<span class="switch-active" aria-hidden="true">On</span>
<span class="switch-inactive" aria-hidden="true">Off</span>
</label>
</div>
<%= f.fields_for :group_member_roles do |group_member_roles| %>
<h4>Group Member Roles</h4>
<%= group_member_roles.label :process, "Turn On/Off Processing" %>
<div class="switch small">
<%= group_member_roles.check_box :process, class: "switch-input" %>
<label class="switch-paddle" for="inbox_group_member_roles_process">
<span class="show-for-sr">Group Member Roles Processing</span>
<span class="switch-active" aria-hidden="true">On</span>
<span class="switch-inactive" aria-hidden="true">Off</span>
</label>
</div>
<%= group_member_roles.label :action, class: "hide" %>
<%= group_member_roles.select :action, ["Add", "Delete"], { selected: "Add" }, { class: "hide" } %>
<%= group_member_roles.label :values %>
<%= group_member_roles.text_field :values, placeholder: "1234, 1337, 1986" %>
<% end %>
Thanks in advance for any help or guidance.
The fields were being stored as a hash and the field was looking for an object to populate with so I added an OpenStruct dummy object to the fields_for to make it so. If anyone can think of a better way please let me know as this is pretty ugly code.
<%= f.fields_for :group_member_roles, OpenStruct.new(f.object.group_member_roles) do |group_member_roles| %>

Rails: Checking value selected in a drop-down and comparing to a value in database

I'm fairly new to Ruby on Rails and I'm building a customer database for my father's landscaping company as a pet project. I've run into a roadblock with some code.
I've got three tables interacting with each other here: Clients, Invoices and Services (nested under Invoices). In the clients table, there are prices stored for each service performed (e.g., cut, bushes, mulch). If the client doesn't receive that service, the entry is null. I've stored the prices this way because each client is charged a different set amount for a service depending on the size of their property.
When adding a service to an invoice, I want to check the service selected in the drop-down against the price set in the client table, and give an error if its null (if the customer doesn't receive that service). For example, if "Cut" is selected, I'd like to compared that to (I think) #invoice.client.cut
I'm not entirely sure where to start with this. How would you have code like this run when the submit button is clicked?
Thanks in advance for any and all help!
Here's what the form looks like in its current state:
<%= form_for [#invoice, #service] do |f| %>
<div class="control-group">
<%= f.label :name %>
<div class="controls">
<%= f.select(:name, options_for_select([['Cut', 'Cut'], ["Mulch", "Mulch"],['Bushes', 'Bush'], ['Spring clean-up', 'Spring'], ['Fall clean-up', 'Fall'], ['Snow removal', 'Snow']])) %>
</div>
</div>
<div class="control-group">
<%= f.label :category %><br>
<div class="controls">
<%= f.select(:category, options_for_select([["Maintenance", "Maintenance"], ["Seasonal", "Seasonal"]])) %>
</div>
</div>
<div class="field">
<%= f.label :quantity %><br>
<%= f.number_field :quantity %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I would add at data-attribute somewhere in the view that lists the customer's current services, possible on the label for that dropdown. You can then use javascript to verify that the option selected is available to the user without making a call to the database.
<%= f.label :name, 'data-services' => #invoice.client.services %>
Instead of checking the validity of the selected value when the user chooses it, don't even show the value in the dropdown. The user experience is different than you described, but not necessarily worse.
The architecture of querying the different kinds of services a client has should be improved by an extra model, but using the methods you already have:
<%= f.select(
:name,
options_for_select(
[
['Cut', 'Cut'],
['Mulch', 'Mulch'],
['Bushes', 'Bush'],
['Spring clean-up', 'Spring'],
['Fall clean-up', 'Fall'],
['Snow removal', 'Snow']
].select do |a|
#invoice.client.public_send(a.last.downcase)
end
)
) %>

How can I set a selected box values for form

Sorry for this rather simple question, but I know almost nothing of rails.
What I want to do is that when I'm registering an "Articulo" I would like to choose some default values that they would be given by an attribute of a model, specifically, the attribute "rut_prov" from the model "Proovedor".
This is what I have.
articulos_controller.rb
def new
#articulo = Articulo.new
#proveedors = Proveedor.all
respond_with(#articulo)
end
articulos/new.html.erb
<div class="container">
<h1>Nuevo artículo de venta</h1>
<%= render 'form' %>
articulos/_form.html.erb
<div class="field">
<%= f.label :rut_del_proveedor %><br>
<%= f.select :rut_prov, #proveedors, :class => 'genForm_dropBox' %>
</div>
view
So, I know the problem is <%= f.select :rut_prov, #proveedors, :class => 'genForm_dropBox' %>, but I don't know how to make appear as options the values of "rut_prov" of the records I have registered of the model "Proovedor".
You can use collection_select method. The following example displays the result of rut_prov method call on view and passes rut_prov of selected as param:
<%= f.collection_select :rut_prov, #proveedors, :rut_prov, :rut_prov, class: 'getForm_dropBox' %>
BTW, I strongly recommend using English variables/classes/methods names. So it should be Provider instead of Provedoor, Article instead of Articulo etc.

Rails editing form with selects

I've created a form in my Rails project with scaffold.
All the fields are strings or text, and some of them I've declared in the view as select fields, for example:
<div class="field">
<%= f.label :device %><br />
<%= f.select :device, options_for_select([["iPhone", "iPhone"], ["iPad", "iPad"], ["All", "All"]]) %>
</div>
Creating and viewing the elements there's no problem. But when I'm editing the fields, the loaded values in the form are the default, not the saved values.
How to solve it?
You are incorrectly using options_for_select, which returns HTML options text, when in fact the f.select call just wants the array.
This will work for you:
<div class="field">
<%= f.label :device %><br />
<%= f.select :device, [["iPhone", "iPhone"], ["iPad", "iPad"], ["All", "All"]] %>
</div>

Resources