Materialize plugin breaks date and datetime selectors - ruby-on-rails

I've been breaking my head with the following problem... I have a basic and functional rails app to record events, I used scaffolds to build the model and all, users can record events with their datetime, everything works. However, as soon as I add "Materialize" to the app, it breaks all the datetime, date, and time selectors in forms.
After Materialize, the datetime_selector generated by Rails in my form just aren't actionable at all and they seem weird (Screenshots included)
– This happens if I install the gem manually or if I install it with bower. I am aware that materialze has a special "datepicker" class for date_selectors. Using this makes no difference.
– I have no other CSS being applied to my form (I removed all scaffolds.css). If I try applying material design styles manually, the forms do work, but I can't use Materialize then.
Any thoughts on why this happens and how to fix it?
Screenshots:
<%= form_for(#event) do |f| %>
<% if #event.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#event.errors.count, "error") %> prohibited this event from being saved:</h2>
<ul>
<% #event.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :datetime %><br>
<%= f.datetime_select :datetime, class: "datepicker" %>
</div>
<div class="field">
<%= f.label :date %><br>
<%= f.date_select :date, class: "datetimepicker-rails" %>
</div>
<div class="field">
<%= f.label :time %><br>
<%= f.time_select :time %>
</div>
<div class="field">
<%= f.label :address %><br>
<%= f.text_field :address %>
</div>
<div class="field">
<%= f.label :cost %><br>
<%= f.text_field :cost %>
</div>
<div class="field">
<%= f.label :category %><br>
<%= f.text_field :category %>
</div>
<div class="field">
<%= f.label :tags %><br>
<%= f.text_field :tags %>
</div>
<div class="field">
<%= f.label :image %><br>
<%= f.text_field :image %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

Hey! So I figured it out and I'm posting this here for anyone who has the same problem.
If you use Materialize with a f.date_select in rails, it simply renders some unusable pixels on your view. Materialize documentation does mention that they use a special class for a date selector class="datepicker". And so the Materialize documentation mentions the following usage for plain html:
<input type="date" class="datepicker">
Which in ERB is equivalent to:
<%= f.date_select :date, class: "datepicker"%>
Which I obviously tried to no avail, and so I was thrown off by this, until I decided to inspect the code (the relevant part of which I list below) for the example in the Materialize website and found the code to be slightly different to their instructions...
<input type="text" class="datepicker"> #Notice the input type
which in ERB is:
<%= f.text_field :date, class: "datepicker"%>
And Voilà!! the date selector now works.

I would add to Carlos answer this, remember to initialize the Javascript using:
$('.datepicker').pickadate({
selectMonths: true, // Creates a dropdown to control month
selectYears: 15 // Creates a dropdown of 15 years to control year});
And for the form:
<%= f.text_field :date_in, class:"datepicker" %>
Hope it helps! this still working in materialize-sass (0.97.6)

I ran into this issue also. For some reason, the datetime select objects were set to display: none after adding materialize CSS
Here is my workaround. Add your own class around the datetime form
<div class="datetime">
<%= f.label :time %><br>
<%= f.datetime_select :time %><br>
</div>
Then declare your own CSS in your application.css file
.datetime select {
display: inline-flex;
width: auto;
}

Related

RoR scaffold error, 'undefined method to_datetime for 0:Fixnum'

I'm new on RoR and then to scaffold a table with datetime type, the following error appear when I enter to generated 'New' page form:
undefined method `to_datetime' for 0:Fixnum
<%= form_for(#alumno) do |f| %>
<% if #alumno.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#alumno.errors.count, "error") %> prohibited this alumno from being saved:</h2>
<ul>
<% #alumno.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :nombres %><br>
<%= f.text_field :nombres %>
</div>
<div class="field">
<%= f.label :apellido_paterno %><br>
<%= f.text_field :apellido_paterno %>
</div>
<div class="field">
<%= f.label :apellido_materno %><br>
<%= f.text_field :apellido_materno %>
</div>
<div class="field">
<%= f.label :dni %><br>
<%= f.text_field :dni %>
</div>
<div class="field">
<%= f.label :usuario %><br>
<%= f.text_field :usuario %>
</div>
<div class="field">
<%= f.label :usuario_personal %><br>
<%= f.text_field :usuario_personal %>
</div>
<div class="field">
<%= f.label :pass %><br>
<%= f.text_area :pass %>
</div>
<div class="field">
<%= f.label :fecha_registro %><br>
<%= f.datetime_select :fecha_registro %> /*error happens here*/
</div>
<div class="field">
<%= f.label :fecha_modificacion %><br>
<%= f.datetime_select :fecha_modificacion %>
</div>
<div class="actions">
<%= f.submit %>
</div>
I found similar questions but don't know exactly how repair the issue.
RoR version 4.0.0
ruby version 2.3.1p112
The code you posted doesn't show where the error is occurring, but the bottom line is that you're trying to call to_datetime on an integer value. to_datetime, however, is a member of String, not integer. Whatever you're trying to convert into a DateTime object must be a String.
If you post the code where the error actually occurs, I can give you more specific direction, but what I've posted so far should be enough to solve the problem on your own.

Handle a B model inside the view's form of an A model

I have a classic form_for:
<div class="field">
<%= f.label "Modèle" %>
<%= f.text_field :model, required: true %>
</div>
<div class="field">
<%= f.label "Immatriculation" %>
<%= f.text_field :license_plate, required: true %>
</div>
<div class="field">
<%= f.label "Complément" %>
<%= f.text_field :complement, required: true%>
</div>
<div class="field">
<%= f.label "Puissance CV" %>
<%= f.number_field :horse_power, required: true %>
</div>
<div class="field">
<%= f.label "Indemnité KM" %>
<%= f.number_field :km_compensation, required: true%>
</div>
<div class="actions">
<%= f.submit 'Sauvegarder' %>
</div>
I would like to use another model inside this view, that would also update when the user clicks the submit button. I know this has to do with nested forms but I'm a little confused about how to implement it. Here's the variable from the second model that I would like to add:
<% #trip_distances.each do |t| %>
<%= form_for(t) do |e| %>
<div class="field">
<%= e.text_field t.id_contract %>
<%= e.number_field t.length %>
</div>
<% end %>
Obviously this is not correct. I guess I need to use the field_for method?
Firstly, consider using simple_form gem, as it cleans up a bit the code, and is overall more developer-friendly :)
Then in your view you will have something like
<%= simple_form_for sth do |form| %>
<% form.input :attributeUno %>
<% form.input :length %>
...
Then to add to this form inputs for nested element add simple_fields_for
<% #trip_distances.each do |t|
<%= form.simple_fields_for t do |fields| %>
<% fields.input :length %>
...
And don't forget to add
accepts_nested_attributes_for :something in your model :)
PS: You should also consider using slim or haml to make creating views a bit easier, and also clean them up :)

undefined method for form fields

I'm working on a advanced search form. I am working in views/searches. Now I have attributes created for user profiles that I have been using such as zip code, age, gender, career, religion, education, etc. I want to use these fields for my advanced search.
When I include the f.label and text fields I get a undefined method. I'm hoping I don't have to recreate each attribute for the search form, as that would not make much sense considering I have already done all these attributes for the user profile. Any help would be greatly appreciated!
/searches/new.html (for search):
<%= form_for #search do |f| %>
<div class="field">
<%= f.label :keywords %><br/>
<%= f.text_field :keywords%>
</div>
<div class="field">
<%= f.label :zip_code %><br/>
<%= f.text_field :zip_code %>
</div>
<div class="actions"><%= f.submit "Search" %></div>
<% end %>
/users/new.html (for the user profile):
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br/>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br/>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :username %><br/>
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :zip_code %><br/>
<%= f.text_field :zip_code %>
</div>
</div>
<div class="field">I'm a
<%= f.select :gender, ['man', 'woman']%>
interested in
<%= f.select :gender, ['women', 'men', 'both']%>
</div>
<div class="field">
Age <%= select(#object, :age, (18..75).to_a) %> to <%= select(#object, :age, (18..75).to_a) %>
</div>
<div class="actions"><%= f.submit %></div>
<% end %>
Your #search object is a Search object (or whatever it actually is), not a User.
Rails doesn't know your Search object doesn't actually have those fields, so when it tries to retrieve the fields that don't exist, it'll blow up.
There are any number of ways around this, including giving your Search a User property.
You could also create a new User and pass it to a user form partial as f, that's probably the approach I would take, although I don't know precisely what it would look like without trying it.

Adding bootstrap-datepicker-rails to date_select in form

So, I have a form that uses 'date-select', which renders three select boxes per used date-select (year, month and day).
Instead I want to use datepicker, and I've followed the instructions here. However, I don't have a clue how to actually implement the datepicker in the form in the view.
I'm using the following line in application.js...
$('.datepicker').datepicker()
...so I guess I need to give the select boxes the class .datepicker?
<%= form_for #project do |f| %>
<div class="text_field">
<%= f.label :title%>
<%= f.text_field :title%>
</div>
<div class="text_field">
<%= f.label :description%>
<%= f.text_field :description%>
</div>
<div class="dropdown">
<%= f.label :start_date%>
<%= date_select :start_date, :startdate %>
</div>
<div class="dropdown">
<%= f.label :end_date%>
<%= date_select :end_date, :enddate%>
</div>
<div class="select">
<%= f.label :title%>
</div>
<div class="submit">
<%= f.submit "Spara" %>
</div>
<% end %>
Try :
<%= date_select :start_date, :startdate , :class => 'datepicker' %>
EDIT: I have replicated your code on my machine and found the possible mistakes:
This gem selects the date in a text field, not in a date-select . The input should be text_field and it looks like this:
<%= f.text_field :date, :class => 'datepicker' %>
As described in the gem's install guide , you should be sure to require it both in your application.css and application.js . One more important thing : in your projects.js.coffee make sure that the DOM is loaded , like this :
jQuery ->
$('.datepicker').datepicker()

checkbox in form associated with Model in Ruby on rails

I have a Model "Startup" and I have a other Model "Categorie". The two tables are associated.
I'd like call the data of Categoria through to form of Startup, this categories displayed with a checkbox. Inside the form Startup Form I have the categorie_id. This is the code
<%= form_for(#startup) do |f| %>
<% if #startup.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#startup.errors.count, "error") %> prohibited this startup from being saved:</h2>
<ul>
<% #startup.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.collection_select :round_id, Round.order(:name), :id, :name, include_blank: true %>
</div>
<div class="field">
<%= f.label :category %><br />
<%= f.text_field :category_id %>
</div>
<div class="field">
<%= f.collection_select :country_id, Country.order(:name), :id, :name, include_blank: true %>
</div>
<div class="actions">
<%= f.submit %> </div> <% end %>
How to display the data of Categories within form with a checkboxs ?
Any idea.
pdt: My english is really bad.
If a Startup can have only one Category, you can do like this in your view:
<div class="field">
<%= f.label :category %><br />
<%= f.collection_select :category_id, Category.all, :id , :name %>
</div>
This will output a dropdown menu with all the categories. Make sure that the Category model has the attribute name.
As you said, a Startup belongs to one Category, so using radiobuttons (checkboxes are here for multiple relation, means you could choose multiple categories):
<div class="field">
<% Category.all.each do |category| %>
<%= f.radio_button :category_id, category.id %>
<%= f.label :category_id, category.name %>
<% end %>
</div>
You may have to add <br /> tags, and html options to make it looks better.
As MrYoshiji says, you can use:
It will display the category name on the screen, and the value sent will be the id.
You can find more details here : http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
I advise you to use the simple_form gem to generate your form. It's a very simple gem to use and customize ! look at it ;)

Resources