ActionView::TemplateMissing error in Rails 4 when rendering shared errors - ruby-on-rails

I'm learning Rails and am implementing an app for which I decided to go through the Site Point tutorial.
According to it, I've done the following in apps/views/users/new.html.erb
<%= form_for #user do |f| %>
<%= render 'shared/errors', object: #user %> #=> error
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, placeholder: 'Your display name', class: 'form-control', required: true %>
</div>
<% end %>
But I'm getting
ActionController:MissingTemplate in Users#new
How can I get rid of it? I've posted the full trace here

The tutorial you're following assumes you have an _errors.html.erb file in the app/views/shared directory.
<%= render 'shared/errors', object: #user %>
That line of code is trying to render that partial and pass #user which will be used in the partial.
If you want to remove that error then remove that line of code. If you want to display those errors then create the relevant file and read up on how to display errors in the view.

<% if object.errors.any? %>
<div id="error_explanation">
<ol>
<% object.errors.full_messages.each do |msg| %>
<li class="alert alert-danger"><%= msg %></li>
<% end %>
</ol>
</div>
<% end %>
Put this in your app/views/shared/_errors.html.erb

Related

Ruby on Rails: Encountered a syntax error while rendering template:

Good night.
I'm doing the getting started guide of RoR, but I have a trouble:
When I click the New Article button that redirects to the form, Rails throw an 'Encountered a syntax error while rendering template.'
The new.html.erb code is
<h1>New Article</h1>
<%= form_with scope: :article, url: articles_path, local: true do |form| %>
<%= 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 %>
<P>
<%= form.label :title %><br>
<%= form.text_field :title %>
</p>
<p>
<%= form.label :text %><br>
<%= form.text_area :text %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
<%= link_to 'Back', articles_path %>
The code is the same as the guide's code, so i don't know where the error or errors can be.
I also attach the error screenshot if it's any help.
Rails error message when I want tho create a new Article
Here two problems:
wrong syntax in if condition (don't need to use =)
extra \n chars in your screenshot
Change your 5 line in app/view/articles/new.html.erb to:
<% if #article.errors.any? %>
<% %> executes Ruby code
<%= %> executes Ruby code and prints out
Also read about the difference on SO

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 nested resource routing error

I have a routing error with a nested resource. Here is my nested routing:
resources :users do
resources :pages
end
This is my minitest "visit new user page" system test:
test "visit new user page path" do
user = User.create
visit new_user_page_path(user)
assert_selector "h1", text: "Page"
end
This fails with the following error:
Error:
PagesTest#test_visit_new_user_page_path:
ActionView::Template::Error: undefined method `pages_path' for #<#<Class:0x00007fa0299dfa28>:0x00007fa02aa23df8>
Did you mean? image_path
app/views/pages/_form.html.erb:1:in `_app_views_pages__form_html_erb__3658586168370814469_70162960780560'
app/views/pages/new.html.erb:3:in `_app_views_pages_new_html_erb__3548077654233884011_70162934875400'
I realize that pages_path is not a correct path for this nested resource. The correct path to pages#new is new_user_page_path(#user) (which is the path that took me to new.html.erb). The correct path to pages#create is a POST to user_pages_path(#user) (which is the page that new.html.erb should POST to). But I cannot find anywhere that pages_path is being called. The error says that it is being called in new.html.erb and also _form.html.erb. Here are those pages. First, new.html.erb:
<h1>New Page</h1>
<%= render 'form', page: #page %>
<%= link_to 'Back', user_pages_path(#page) %>
And _form.html.erb:
<%= form_with(model: page, local: true) do |form| %>
<% if page.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(page.errors.count, "error") %> prohibited this page from being saved:</h2>
<ul>
<% page.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :title %>
<%= form.text_field :title, id: :page_title %>
</div>
<div class="field">
<%= form.label :content %>
<%= form.text_area :content, id: :page_content %>
</div>
<div class="field">
<%= form.label :user_id %>
<%= form.text_field :user_id, id: :page_user_id %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
I don't know where pages_path is being called, so I can't fix that error. Any help is appreciated.
You need to change your form code, page_path is called from it. Should be
<%= form_with(model: [ #user, #page ]) do |form| %>
In this case the route will be set correctly.

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