When user clicks AddDate in my event planning app, I add a row as seen below so user can add a date and start/end time, and AM/PM.
But in my form using nested attributes, it seems my select controls need a reference to the model field they set (:start_time and :end_time). But the start and end times are create from TWO select controls, one for the hours and one for the minutes. So I'm not sure how the values chosen in the two selects will be combined to form the start and end times.
<div class="user_event_inline_container margin_left_ten padding_right_gone">
<%= f.label :start_time, "Start", class: 'info_inline_control info_label five_margin_right' %>
<%= f.select :start_time, options_for_select([['1',1],['2',2],['3',3],['4',4],['5',5],['6',6],['7',7],['8',8],['9',9],['10',10],['11',11],['12',12]]), class: (field_class(#user_event, :start_time) + 'info_inline_control') %>
<%= f.select :start_time, options_for_select([['00',1],['15',2],['30',3],['45',4]]), class: (field_class(#user_event, :start_time) + 'info_inline_control') %>
<%= f.select :start_am_pm, options_for_select([['am',1],['pm',2]]), class: (field_class(#user_event, :start_am_pm) + 'info_inline_control') %>
</div>
<div class="user_event_inline_container margin_left_ten padding_right_gone">
<%= f.label :end_time, "End", class: 'info_inline_control info_label five_margin_right' %>
<%= f.select :end_time, options_for_select([['1',1],['2',2],['3',3],['4',4],['5',5],['6',6],['7',7],['8',8],['9',9],['10',10],['11',11],['12',12]]), class: (field_class(#user_event, :end_time) + 'info_inline_control') %>
<%= f.select :end_time, options_for_select([['00',1],['15',2],['30',3],['45',4]]), class: (field_class(#user_event, :end_time) + 'info_inline_control') %>
<%= f.select :end_am_pm, options_for_select([['am',1],['pm',2]]), class: (field_class(#user_event, :end_am_pm) + 'info_inline_control') %>
</div>
Why don't you use a time_select instead?
Doing it this way, you'll need to merge them manually in your controller using params[] values and Date.parse().
You can also try using f.time_select, which should function as a normal field after you submit.
Might find this useful: Where is the Rails method that converts data from `datetime_select` into a DateTime object?
Personally, I write custom interfaces for my date fields using jQuery plugins and then use javascript to merge all the fields into a hidden field that actually gets submitted.
Related
In my Ruby on Rails application I have some form that needs inputs to select month and year. I do this with built in Rails select_date input:
= select_date #date, :order => [ :month, :year], :discard_day => true
But now I want to know is there any way to the same easy with simple_form?
Obviously an old question but the answer is YES, you absolutely can do the same with simple_form. Posted for others looking for this answer.
You'll pass date as the input type and then rather like the select_date form_for input helper, you'll pass discard_day
So it might look like this:
= f.input :your_date_field, as: :date, discard_day: true
Hello read a this documentation
1.4 Other Helpers of Interest
Other form controls worth mentioning are textareas, password fields, hidden fields, search fields, telephone fields, date fields, time fields, color fields, datetime fields, datetime-local fields, month fields, week fields, URL fields, email fields, number fields and range fields:
<%= text_area_tag(:message, "Hi, nice site", size: "24x6") %>
<%= password_field_tag(:password) %>
<%= hidden_field_tag(:parent_id, "5") %>
<%= search_field(:user, :name) %>
<%= telephone_field(:user, :phone) %>
<%= date_field(:user, :born_on) %>
<%= datetime_field(:user, :meeting_time) %>
<%= datetime_local_field(:user, :graduation_day) %>
<%= month_field(:user, :birthday_month) %>
<%= week_field(:user, :birthday_week) %>
<%= url_field(:user, :homepage) %>
<%= email_field(:user, :address) %>
<%= color_field(:user, :favorite_color) %>
<%= time_field(:task, :started_at) %>
<%= number_field(:product, :price, in: 1.0..20.0, step: 0.5) %>
<%= range_field(:product, :discount, in: 1..100) %>
http://guides.rubyonrails.org/form_helpers.html
and more example examples here:
http://apidock.com/rails/ActionView/Helpers/DateHelper/date_select
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.
I am trying to allow a user to input two different things in two different drop down menus from the same form and it will store an integer into a review table.
I want the user to be able to select model_name in one drop down and manufacturer in another drop down. The result will store a bat_id integer into the form. (Telling you which bat the user is selecting)
I have seen a couple questions about date & time but they store the values directly in the model. I am trying to store an integer - bat_id so that the bat_id will directly link the review model to the bat model.
Examples I have found that are close:
How do ruby on rails multi parameter attributes really work (datetime_select)
Rails multiple fields to one model attribute
Using multiple input fields for one attribute
Rails Update Single Attribute with Multiple Fields
My form now:
<%= form_for(#review) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field" align= "center">
<h3>Select Brand</h3>
<%= f.collection_select :manufacturer_id, Manufacturer.all, :id, :manufacturer, include_blank: true %>
<h3>Select Bat</h3>
<%= f.grouped_collection_select :bat_id, Manufacturer.all, :bats, :manufacturer, :id, :model_year_and_name, include_blank: true %>
<h3>What do you like about this bat?</h3>
<%= f.text_area :pros, placeholder: "Enter what you like..." %>
<h3>What do you not like about this bat?</h3>
<%= f.text_area :cons, placeholder: "Enter what you don't like..." %></br>
</div>
<div align="center">
<%= f.submit "Add Review", class: "btn btn-large btn-info" %>
</div>
<% end %>
I am submitting to the review table and trying to submit both of these to the bat_id attribute.
<h3>Select Brand</h3>
<%= f.collection_select :manufacturer_id, Manufacturer.all, :id, :manufacturer, include_blank: true %>
<h3>Select Bat</h3>
<%= f.grouped_collection_select :bat_id, Manufacturer.all, :bats, :manufacturer, :id, :model_year_and_name, include_blank: true %>
In my bat model I have: has_many :reviews & In my reviews model I have: belongs_to :bat
UPDATE: Is it possible to use a hidden field with the combination of javascript and my two inputs to determine my one output bat_id?
Update I changed my dropdown code to what works so that I enter in manufacturer_id & bat_id when both are selected. However I still think there is a way to store one value in my review model. I am using javascript very similiar to this
From a UI perspective this seems broken... users will be able to associate any model year & name with any manufacturer, even if that manufacturer did not produce that model year & name.
Assuming you will introduce some javascript to handle that, from a rails perspective you will get undefined behavior with two :bat_id fields in the same form. I think you need this:
<h3>Select Brand</h3>
<%= f.collection_select :manufacturer_id, Manufacturer.all, :id, :manufacturer, include_blank: true %>
<h3>Select Bat</h3>
<%= f.collection_select :bat_id, Bat.all, :id, :model_year_and_name, include_blank: true %>
Alternatively you can just create one dropdown containing a composite field, like this:
<h3>Select Bat</h3>
<%= f.collection_select :bat_id, Bat.all.sort {|a, b| a.manufacturer_model_year_and_name <=> b.manufacturer_model_year_and_name}, :id, :manufacturer_model_year_and_name, include_blank: true %>
and then in your Bat model introduce something like this:
def manufacturer_model_year_and_name
"#{self.manufacturer.name}: #{self.model_year_and_name}"
end
As discussed in your other answer, you shouldn't need to store the manufacturer_id on your review model.
I would recommend creating a Manufacturer select that isn't accessed in your Review model, but is simply used to filter the list of bats on the form.
The best way to do this is probably to add some custom data attributes to the Bat select.
<%= collection_select :manufacturer, :manufacturer_id, Manufacturer.all, :id, :manufacturer %>
<%= f.select :bat_id, Bat.all.map{ |b| [b.model_year_and_name, b.id, {'data-manufacturer' => b.manufacturer_id}] } %>
Then use some javascript to filter the Bat select when the Manufacturer select is changed.
Unfortunately you cannot just set display: none to an option element to hide it. This does not hide the option in many browsers. So the best method is to use a bit of jQuery to clone the original select every time the manufacturer select is changed, and remove any option that isn't associated with the selected manufacturer. Like so:
// rename the original select and hide it
$('#bat_id').attr('id', 'bat_id_original').hide();
$('#manufacturer_id').on('change', function() {
$('#bat_id').remove(); // remove any bat_id selects
$bat = $('#bat_id_original')
.clone() // clone the original
.attr('id', 'bat_id') // change the ID to the proper id
.insertAfter('#bat_id_original') // place it
.show() // show it
.find(':not(option[data-manufacturer="' + $(this).val() + '"])')
.remove(); // find all options by other manufacturers and remove them
});
You might need to change a few things to get this to work in your installation, but you can view a static demo on jsFiddle here: http://jsfiddle.net/JL6M5/
You will probably need to reject the manufacturer_id field on form submit, avitevet already pointed out this answer which should help there: Rails: Ignoring non-existant attributes passed to create()
I am trying to link multiple rails models in my app. I am trying to let users create reviews on a product using a form. I am trying to use the rails DRY principle.
I first made a bat table with bat name, model year, and an image. Then I created a manufacturer table which only lists the names of the manufacturers of bats. My bats model belongs_to :manufacturer and my manufacturer model has_many :bats.
Instead of creating multiple tables using manufacturer, (listing the name of the manufacturer at least 3 times for each bat) how can I link my two models together?
My form is submitted to the review model. In the form I already have <%= f.collection_select :bat_id, Manufacturer.all, :id, :manufacturer, include_blank: true %>, which lists all of the possible manufacturers in a drop down menu. HOWEVER, nothing is submitted to the :bat_id parameter in the review form when submitted.
--One guess is to have the manufacturer_id integer stored in the bat model as an integer under the column manufacturer_id(Note: already done this, but I don't know how to submit that in a form?)
--Another guess is to have the bat model inherit from the manufacturer model
Any help is greatly appreciated
My full form:
<%= form_for(#review) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field" align= "center">
<h3>Select bat</h3>
<%= f.collection_select :bat_id, Manufacturer.all, :id, :manufacturer, include_blank: true %>
<h3>What do you like about this bat?</h3>
<%= f.text_area :pros, placeholder: "Enter what you like..." %>
<h3>What do you not like about this bat?</h3>
<%= f.text_area :cons, placeholder: "Enter what you don't like..." %></br>
</div>
<div align="center">
<%= f.submit "Add Review", class: "btn btn-large btn-info" %>
</div>
<% end %>
according to the documentation
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
the params in the collection_select is supposed to be:
f.collection_select(:post, :author_id, Author.all, :id,
:name_with_initial, prompt: true)
where post is the Model,author_id is the attribute
or you could try using:
=f.select( :bat_id, options_from_collection_for_select(#manufacturers,"id","manufacturer",
f.object. bat_id),{})
and put the
#manufacturers = Manufacturer.all
inside the controller
I'm in trouble with a form I'm doing on rails 3.
What I want to do is:
I want to pass an extra parameter only to decide how many builds you want to do
to the next form. I'm trying to pass the value through a text_field_tag, but I can't get it
on the controller side.
This is what I've done:
Model:
class Story < ActiveRecord::Base
attr_accessible :resume,
:title,
:prelude,
:chapter_numbers
attr_accessor :chapter_numbers
# etc etc etc
end
View:
<%= simple_form_for(#story) do |f| %>
<div class="field">
<%= f.input :prelude, as: :text, input_html: { rows: 10, style: 'width: 100%' } %>
</div>
<div class="field">
<%= text_field_tag :chapter_numbers %>
</div>
<% end %>
the extra parameter is :chapter_numbers, which I want to catch in the controller as
params[:chapter_numbers], but it's not working. Tried to add it as virtual attribute (don't know if it's necessary)
Thanks in advance!
text_field_tag is an independent field, and won't be sent in your params
text_field_tag And text_field Are Different
You'd need to use f.text_field because this will send the required params to your controller, like this:
<%= f.text_field :chapter_numbers %>
or in your case (with simple form):
<%= f.input :chapter_numbers, as: :text %>
Good resource here about this