Rails Display Form Error Below Each Field - ruby-on-rails

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>

Related

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.

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.

Showing multiple iterations of form fields when in edit file of scaffold

First, I have a model called Answers for my trivia game. It stores the multiple possible answers there are to each trivia question (a quiz has_many answers). I created a form via scaffold to make an easy UI for submitting a question with a set of four answers.
I want to do this from one form. When the user currently hits submit, I can post all four answers -- each with a different answer_id but sharing the same question_id (so I can associate 4 answers with one question) -- successfully, like so:
<%= form_for(#question) do |f| %>
<% if #question.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#question.errors.count, "error") %> prohibited this question from being saved:</h2>
<ul>
<% #question.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<div class="field">
<%= f.label :question_text %><br>
<%= f.text_field :question_text, class: "form-control" %>
</div>
</div>
<div class="form-group">
<div class="field">
<%= f.label :category_id %><br>
<%= f.number_field :category_id, class: "form-control" %>
</div>
</div>
<h2>Answer Options</h2>
<%= f.fields_for :answers do |answer| %>
<div class="form-group">
<div class="answers">
<div class="field">
<%= answer.label :answer_1 %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
</div>
</div>
</div>
<% end %>
<%= f.fields_for :answers do |answer| %>
<div class="form-group">
<div class="answers">
<div class="field">
<%= answer.label :answer_2 %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
</div>
</div>
</div>
<% end %>
<%= f.fields_for :answers do |answer| %>
<div class="form-group">
<div class="answers">
<div class="field">
<%= answer.label :answer_3 %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
</div>
</div>
</div>
<% end %>
<%= f.fields_for :answers do |answer| %>
<div class="form-group">
<div class="answers">
<div class="field">
<%= answer.label :answer_4 %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
</div>
</div>
</div>
<% end %>
If I check in console, it works! I have four different answers (so they are four rows in the table but with the same question_id. What is weird is if I then use the edit route, instead of populating four inputs (like new does), it shows 16 text_field inputs for Answers (labeled Answer 1, Answer 2, Answer 3, Answer 4, Answer 1).
Finally, the other reason I think there could be an issue would be how I am updating the nested Answers attribute through the controller, like so:
def question_params
params.require(:question).permit(:question_text, :category_id, :correct_answer, :answers_attributes => [:id, :answer_text])
end
Am I setting this up incorrectly, such that it would iterate 4x when showing in the Edit file?
I think you need to do little different while rendering the form for edit action:
You can try something like this:
<%= form_for(#question) do |f| %>
<% if #question.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#question.errors.count, "error") %> prohibited this question from being saved:</h2>
<ul>
<% #question.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title, class: "form-control" %>
</div>
</div>
<% unless #question.new_record? %>
<% #question.answers.each_with_index do |ans,i| %>
<%= f.fields_for :answers, ans do |answer| %>
<%= answer.label "answer_#{i+1}" %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
<% end %>
<% end %>
<% else %>
<h2>Answer Options</h2>
<%= f.fields_for :answers, Answer.new do |answer| %>
<%= answer.label :answer_1 %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
<% end %>
<%= f.fields_for :answers,Answer.new do |answer| %>
<%= answer.label :answer_2 %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
<% end %>
<%= f.fields_for :answers,Answer.new do |answer| %>
<%= answer.label :answer_3 %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
<% end %>
<%= f.fields_for :answers,Answer.new do |answer| %>
<%= answer.label :answer_4 %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
<% end %>
<% end %>
<%= f.submit #question.new_record? ? "Add" : "Update" %>
<% end %>

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.

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