Showing associated model errors in Rails 6 form - ruby-on-rails

I'm using Rails 6.0.3.4 and Ruby 2.7.2. Using the Rails getting started tutorial as an example, I'm wondering how to show form validation errors of an associated model.
Show page
<p>
<strong>Title:</strong>
<%= #article.title %>
</p>
<p>
<strong>Text:</strong>
<%= #article.text %>
</p>
<h2>Comments</h2>
<%= render #article.comments %>
<h2>Add a comment:</h2>
<%= render 'comments/form' %>
<%= link_to 'Edit', edit_article_path(#article) %> |
<%= link_to 'Back', articles_path %>
Comments form (this is the form in question)
<%= form_with(model: [ #article, #article.comments.build ], local: true) do |form| %>
<p>
<%= form.label :commenter %><br>
<%= form.text_field :commenter %>
</p>
<p>
<%= form.label :body %><br>
<%= form.text_area :body %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
Models
class Comment < ApplicationRecord
belongs_to :article
validates :commenter, presence: true
end
class Article < ApplicationRecord
has_many :comments, dependent: :destroy
validates :title, presence: true,
length: { minimum: 5 }
end
For a single model articles form, errors could be shown like this.
<% if #article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#article.errors.count, "error") %> prohibited
this article from being saved:
</h2>
<ul>
<% #article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
How do I show errors for the comments form? When I submit a comment without a commentor, it does not save, so validations are happening, but I'm not sure how to show errors for this type of form.
<% if #???????.errors.any? %> ###### What do I say here to get the comment errors?
<div id="error_explanation">
<h2>
<%= pluralize(#?????.errors.count, "error") %> prohibited
this article from being saved:
</h2>
<ul>
<% #?????.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

1. Create a reusable partial for errors
# app/views/shared/_errors.html.erb
<div class="error_explanation">
<h2><%= pluralize(object.errors.count, "error") %> prohibited
this <%= object.model_name.singular %> from being saved:</h2>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
And a little helper method:
# app/helpers/application_helper.rb
module ApplicationHelper
# Displays the errors for a model instance if there are any
def display_errors_for(object)
return unless object.errors.any?
render partial: 'shared/errors',
locals: { object: object }
end
end
2. Get the object from the form builder
You can always access the model wrapped by the form builder instance through the #object method instead of using a instance variable:
<%= form_with(model: [ #article, #comment ], local: true) do |form| %>
# ...
<%= display_errors_for(form.object) %>
<% end %>
And like magic you can add errors to any form with a single line.
Do not use #article.comments.build. That will always bind the form to a new instance of comment instead of displaying the errors! It will also remove anything the user entered into the form... Assign the variable in the controller. I have no idea how this snuck its way into the guides.
class ArticlesController < ApplicationController
def show
#article = Article.find(params[:id])
#comment = #article.comments.new
end
end

Related

is missing a template for request formats: text/html

I have Term and Phrase models and I am adding Nested Resources. I want to get the Term Id for Phraes_term to create a data pair
Example: Term 1 and Phrase 2.
But when you press the new button from show.html.erb term, an error will occur
-->error :PhrasesTermsController#new is missing a template for request formats: text/html
form.html.erb
-(phrases_term).
<%= form_with(model: phrases_term, local: true) do |form| %>
<% if pharases_term.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(pharases_term.errors.count, "error") %> prohibited this phrase from being saved:</h2>
<ul>
<% pharases_term.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :ID %>
<%= form.number_field_tag :ID %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
(phrases_term)
-new.html.erb
<%= render 'form', phrases_term: #phrases_term %>
Phrases_terms_controller.rb
class PhrasesTermsController < ApplicationController
before_action :authenticate_user!
before_action :set_term
def new
#phrases_term = PhrasesTerm.new
end
def create
#phrases_term = #term.phrases_term
#phrases_term.user = current_user
#phrases_term.save
redirect_back(fallback_location: root_path)
end
private
def phrases_term_params
params.require(:phrases_term).permit(:term_id)
end
def set_term
#term = Term.find(params[:term_id])
end
end
routes.rb
resources :terms do
resources :phrases_terms, only: [:create, :destroy, :new]
end
(Term)
Show.html.erb
<td><%= link_to 'New', new_term_phrases_term_path(#term) %></td>
(phrases_term).
_form.html.ebr
<%= form_with(model: phrases_term, local: true) do |form| %>
<% if pharases_term.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(pharases_term.errors.count, "error") %> prohibited this phrase from being saved:</h2>
<ul>
<% pharases_term.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :ID %>
<%= form.number_field_tag :ID %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
(phrases_term)
new.html.erb
<%= render 'form', phrases_term: #phrases_term %>
This means you do not have a app/views/phrases_term(s)/new.html.erb file. I'm unsure if you've namespaced the files under 'phrases_term' or 'phrases_terms'.
Make one in the correct directory, populate it, and it will load.

When I do user.file.attach(params[:file]) it doesn't save when using active storage

I'm getting this error with the line #user.file.attach(params[:file]):
ActiveRecord::RecordNotSaved in UsersController#runFile.
Failed to save the new associated file_attachment.
I am using active storage to store files.
In my User model I have this code:
has_one_attached :file
in my Users Controller,
I have this code:
def runFile
```
#user.file.attach(params[:file])
```
end
```
def user_params
params.require(:user).permit(:file)
end
in my form in views I have this code:
<%= form_with(model: user, local: true) do |form| %>
<% 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 %>
<div class="field">
<%= form.label :file %>
<%= form.file_field :file %>
</div>
<br>
<h6 class="actions">
<%= form.submit "Create" %>
</h6>
<% end %>
This is my show form as for each user you click Run File for the runFile method:
<p id="notice"><%= notice %></p>
<%= link_to "Run File", runFile_path, method: :post %>
<%= link_to 'Edit', edit_user_path(#user) %> |
<%= link_to 'Back', users_path %>
I think you have mixed a few things.
To display a form, you need a normal link (without method). In your case, it will be runFile. However, runFile is responsible just for displaying the form. You need another method that will handle the action of submitting the form. Similar to edit and update methods.
Here is an idea of how it might look:
def runFile_form
# do whatever you need to display the form
end
def runFile
#user.file.attach(params[:file])
end
def user_params
params.require(:user).permit(:file)
end
In routes, please define a GET route for runFile_form method and use it on the show page:
<%= link_to "Run file", runFile_form_path %>

Rails errors not showing?

So im learning rails, and following the rails getting started guide (I've actually done the "blog app" on the Udemy rails course, but im making sure I can write it from scratch first before moving on).
Anyways, i've gotten Delete/Create running, but I was adding validation...and while the validation works my errors aren't showing up.
Right now my pages are super simple:
new.html.erb
<% 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 |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form_with scope: :user, url: users_path, local: true do |form| %>
<p>
<%= form.label :username %><br>
<%= form.text_field :username %>
</p>
<p>
<%= form.label :name %><br>
<%= form.text_field :name %>
</p>
<p>
<%= form.label :age %><br>
<%= form.number_field :age %>
</p>
<p><%= form.submit %></p>
<% end %>
users_controller
class UsersController < ApplicationController
def index
#users = User.all
end
def new
#user = User.new
end
def edit
#user = User.find(params[:id])
end
def create
#user = User.new(params.require(:user).permit(:username,:name,:age))
if #user.save
redirect_to users_path
else
render 'new'
end
end
def update
end
def destroy
#user = User.find(params[:id])
#user.destroy
redirect_to users_path
end
end
So the weird thing if I go into my network tab in dev tools I can see this show up in the response tab:
<div id="error_explanation">
<h2>
1 error prohibited this user from being saved
</h2>
<ul>
<li>Username has already been taken</li>
</ul>
</div>
But it doesn't show up in "elements" in Chrome dev tools. I've restarted rails....so Im really not sure why the elements are not showing up. I DO have bootstrap 4.00 beta installed, but not sure why that would matter. This is rails 5.1.4 btw.
This is because you are not getting the same #user in the <% if #user.errors.any? %>
Try this :
<%= form_with scope: :user, url: users_path, local: true do |form| %>
<% 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 |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= form.label :username %><br>
<%= form.text_field :username %>
</p>
<p>
<%= form.label :name %><br>
<%= form.text_field :name %>
</p>
<p>
<%= form.label :age %><br>
<%= form.number_field :age %>
</p>
<p><%= form.submit %></p>
<% end %>

Instance variable nil in one view, not in another

I'm working on a project in Ruby on Rails (Ruby v.2.2.8, Rails 5.1.4) and have encountered a very strange issue.
For my show method in the controller, I have:
def show
#county = County.find(params[:id])
end
And it works. For update, I have.
def update
#county = County.find(params[:id])
if #county.update(county_params)
redirect_to #county
else
render 'edit'
end
end
In my 'edit', I consistently get an error that #county is nil. The error page indicates that the parameters are being passed as:
{'id'=>4}
as an example. When I use find_by from the rails console, the item is found.
Is there something here I'm missing?
ETA: View Code
<%= form_with model: #county, local: true do |form| %>
<% if #county.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#county.errors.count, "error") %> prohibited
this county from being saved:
</h2>
<ul>
<% #county.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= form.label :name %><br>
<%= form.text_field :name %>
</p>
<p>
<%= form.label :shortname %><br>
<%= form.text_field :shortname %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
ETA Routes for Counties:
counties GET /counties(.:format) counties#index
POST /counties(.:format) counties#create
new_county GET /counties/new(.:format) counties#new
edit_county GET /counties/:id/edit(.:format) counties#edit
county GET /counties/:id(.:format) counties#show
PATCH /counties/:id(.:format) counties#update
PUT /counties/:id(.:format) counties#update
DELETE /counties/:id(.:format) counties#destroy
The error occurs at /counties/:id/edit
How is your edit action in your controller?
You should define #county as well
def edit
#county = County.find(params[:id])
end

Rails: one view, model and it's associated model

So, for example, case from http://guides.rubyonrails.org/getting_started.html As you can see, if you try to create invalid post, you will see error messages:
<%= form_for #post do |f| %>
<% if #post.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(#post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
How to implement error messages rendering for associated Comment model, keeping in mind that comment creation form is placed in posts/show view?
Form code is usually kept in the folder of the matching model in a _form.html.erb partial that is rendered in both new.html.erb and edit.html.erb (to see a good example, generate a scaffold for a sample model).
What you can do in your case is render this comments form partial in the posts show action.
app/views/posts/show.html.erb
<%= render 'comments/form', comment: #comment || #post.comments.build # Whatever you have here %>
app/views/comments/_form.html.erb
<%= form_for comment do |f| %>
<%= render 'error_messages', target: comment %>
...
<% end %>
In addition, showing error messages usually is the same in all forms, so in order to remove duplication, you can extract this code into a seperate partial.
app/views/application/error_messages.html.slim # here is slim syntax, convert as nescessary
/ error explanation
/
/ = render 'shared/error_explanation', target: #school
/
- if target.errors.any?
.error-messages
h4 Please correct the following fields:
ul
- target.errors.full_messages.each do |message|
li = message
Hope this helps.

Resources