RoR scaffold error, 'undefined method to_datetime for 0:Fixnum' - ruby-on-rails

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.

Related

Why am I missing an input in a form after scaffolding in Rails?

I'm learning Rails and I try to use scaffold to generate some code.
Everything seems to be OK after this except I miss an input in one of my forms and I don't know why.
Someone could explain this ?
Here the partial form which generates the view:
_form.html.erb
<%= form_with(model: advertisement, local: true) do |form| %>
<% if advertisement.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(advertisement.errors.count, "error") %> prohibited this advertisement from being saved:</h2>
<ul>
<% advertisement.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :title %>
<%= form.text_field :title %>
</div>
<div class="field">
<%= form.label :content %>
<%= form.text_area :content %>
</div>
<div class="field">
<%= form.label :price %>
<%= form.number_field :price %>
</div>
<div class="field">
<%= form.label :state %>
<%= form.text_field :state %>
</div>
<div class="field">
<%= form.label :user_id %>
<%= form.number_field :user_id %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
The HTML output for this input:
<div class="field">
<label for="advertisement_title">Title</label>
<input type="text" name="advertisement[title]" id="advertisement_title" />
</div>
Thank you everyone
OK I got it!
It's because of the Adblock extension.
On every page where it is enabled, it injects CSS directly into the page and adds a
display: none !important;
for hundreds of defined id's.
Unfortunately for me, #advertisement_title is one of them!
It works when I desactivate it
adblock display none

How do i write my form code for 3rd nested model in Rails 5?

I'm working on an app where i nested the 3 model
resources :genres do
resources :stories do
resources :episodes
end
end
Meanwhile, i've been able to write the form for genres and stories as in
<%= form_for(genre) do |f| %>
<% if genre.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(genre.errors.count, "error") %> prohibited this genre from being saved:</h2>
<ul>
<% genre.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %>
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
and stories
<%= form_for([#genre, #genre.stories.build]) do |f| %>
<div class="field">
<%= f.label :title %>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :summary %>
<%= f.text_area :summary %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Can someone pls tell me how to write the form for episodes...
When i wrote this
<%= form_for([#genre.stories, #genre.#stories.episodes.build]) do |f| %>
<div class="field">
<%= f.label :title %>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %>
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I got this error
syntax error, unexpected tIVAR, expecting '(' ...#genre.stories, #genre.#stories.episodes.build]) do |f| #out... ... ^ C:/Users/Adegbite/Documents/Apps/twiliste2/app/views/episodes/_form.html.erb:1: syntax error, unexpected ']', expecting keyword_end ...#genre.#stories.episodes.build]) do |f| #output_buffer.safe_... ... ^ C:/Users/Adegbite/Documents/Apps/twiliste2/app/views/episodes/_form.html.erb:16: syntax error, unexpected keyword_ensure, expecting end-of-input
Thanks.
This:
<%= form_for([#genre.stories, #genre.#stories.episodes.build]) do |f| %>
Should be more like:
<%= form_for([#genre, #story, #story.episodes.build]) do |f| %>
Of course, you'll have to set #genre and #story appropriately in the new action of your EpisodesController.
BTW, #genre.#stories makes no sense and is not valid. #stories is an instance variable which, presumably, contains (or would contain) one or more stories. .stories is a method call on an instance of Genre. .#stories is nothing.

Date is not working in rails application with mongo id

I have create an rails application with mongoid but im facing one problem at DATE
I created an scaffold with a name "posting"
When im editing date it will updated....
I follow the instruction of Railscast #238 Mongoid here
there is my posting.rb file
class Posting
include Mongoid::Document
field :title
field :description
field :comments
field :published, :type => Date
end
this my _from.html.erb
<%= form_for(#posting) do |f| %>
<% if #posting.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#posting.errors.count, "error") %> prohibited this posting from being saved:</h2>
<ul>
<% #posting.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 :description %><br>
<%= f.text_field :description %>
</div>
<div class="field">
<%= f.label :published %><br>
<%= f.date_select :published %>
</div>
<div class="field">
<%= f.label :comments %><br>
<%= f.text_area :comments %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
and finally my show.html.erb file
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= #posting.title %>
</p>
<p>
<strong>Description:</strong>
<%= #posting.description %>
</p>
<p>
<strong>published:</strong>
<%= #posting.published %>
</p>
<p>
<strong>Comments:</strong>
<%= #posting.comments %>
</p>
<%= link_to 'Edit', edit_posting_path(#posting) %> |
<%= link_to 'Back', postings_path %>
What do you mean by not working? doesn't look like you have used published property in any of your views.
in your show.html.erb you are using
<%= f.date_select :pub %>
and in your show.html.erb you are using
<%= #posting.pub %>
However there is no property called pub in your Posting model. What you have there is called published
field :published, :type => Date
You either need to rename it in the model, or in the views to match.

Rails Display Form Error Below Each Field

I have the following form
<%= form_for(#user) do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= #user.errors.messages[:name] %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<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 :phone %><br>
<%= f.text_field :phone %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I want to display form error for each field below it instead of everything on the top. Went through rails guide but was not able to figure how to do it.
You can write a helper to return error message for any field of any object
In /app/helpers/application_helper.rb:
def show_errors(object, field_name)
if object.errors.any?
if !object.errors.messages[field_name].blank?
object.errors.messages[field_name].join(", ")
end
end
end
In view file:
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
<p class='error'><%= show_errors(#user, :name) %></p>
</div>

undefined local variable or method `f' in Rails and Paperclip

I'm trying to create a new map, and it's not working out so far:
undefined local variable or method `f' for #<#<Class:0x007ff46c0b12c0>:0x007ff46d777ba8>
I'm using Paperclip. The new page form:
<%= form_for #map, :url => maps_path, :html => { :multipart => true } do |form| %>
<% if #map.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#map.errors.count, "error") %> prohibited this map from being saved:</h2>
<ul>
<% #map.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :carname %><br />
<%= f.text_field :carname %>
</div>
<%= form.file_field :map %>
<div class="field">
<%= f.label :criticalcomponentlocations %><br />
<%= f.text_area :criticalcomponentlocations %>
</div>
<div class="field">
<%= f.label :warnings %><br />
<%= f.text_area :warnings %>
</div>
<div class="field">
<%= f.label :additionalinfo %><br />
<%= f.text_area :additionalinfo %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Since you pass form variable to the block passed to form_for method, you should substitute f with form.

Resources