This question already has answers here:
Rails “Template is missing” error
(2 answers)
Closed 5 years ago.
I am beginner in rails
In routes.rb I have
root 'post#index'
resources :posts
When I click on "new post" for the empty title of the post,I get a error like
Missing template posts/new, application/new with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/home/supranimbus12/RoR/instagram_app/app/views" * "/home/supranimbus12/.rvm/rubies/ruby-2.3.0/lib/ruby/gems/2.3.0/gems/devise-4.4.0/app/views"
My posts/new.html.erb file like
<h2>New Post</h2>
<hr>
<%= form_for #post do |f| %>
<% f.label :description %>
<% f.textarea :description %>
<hr>
<% f.submit %>
<% end %>
1- change routes as: -
root 'post#index'
resources :posts, except: [:index]
because you have already defined as root of index action
2- now for for new action
def new
#post = Post.new
end
3- in you app/view/posts/new.html.erb
<h2>New Post</h2>
<hr>
<%= form_for #post do |f| %>
<% f.label :description %>
<% f.textarea :description %>
<hr>
<% f.submit %>
<% end %>
3 - after hitting the submit button it will go to create action,
note: - because form_for object automatic hit to create action if object is new otherwise it will hit the update action that's why we generally use this form as partial to use it for edit and new action both
def create
#post = Post.new(post_params)
if #post.save
flash[:notice] = "post saved successfully!"
redirect_to #post
else
flash[:error] = #post.errors.full_messages.to_sentence
render 'new'
end
private
def post_params
params.require(:post).permit!
end
to permit all data make a private method for strong parameter
post_params
and here redirect_to #post will go to show action if post saved in db,as it will make url like post/:id otherwise it will render to new template again. with error flash message.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
This post was edited and submitted for review 8 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
As I am new to Ruby. after running the localhost URL I am getting this error.
for this line . which is in the show.html.erb file
<%= render #article.comments %>
Showing D:/RailProject/App/app/views/articles/show.html.erb where line #14 raised:
Missing partial comments/_comment with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}.
Searched in:
* "D:/RailProject/App/app/views"
* "C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/actiontext-7.0.3/app/views"
* "C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/actionmailbox-7.0.3/app/views"
I have this code in comments_controller.rb :
class CommentsController < ApplicationController
http_basic_authenticate_with name: "dhh", password: "secret", only: :destroy
def create
#article = Article.find(params[:article_id])
#comment = #article.comments.create(comment_params)
redirect_to article_path(#article)
end
def destroy
#article = Article.find(params[:article_id])
#comment = #article.comments.find(params[:id])
#comment.destroy
redirect_to article_path(#article), status: 303
end
def show
#article = Article.find(params[:article_id])
#comment = #article.comments.find(params[:id])
#comment.destroy
redirect_to article_path(#article)
end
private
def comment_params
params.require(:comment).permit(:commenter, :body, :status)
end end
I have this code in show.html.erb :
<h1><%= #article.title %></h1>
<p><%= #article.body %></p>
<ul>
<li><%= link_to "Edit", edit_article_path(#article) %></li>
<li><%= link_to "Destroy", article_path(#article), data: {
turbo_method: :delete,
turbo_confirm: "Are you sure?"
} %></li>
</ul>
<h2>Comments</h2>
<%= render #article.comments %>
<h2>Add a comment:</h2>
<%= render 'comments/form' %>
I have _comments.html.erb :
<p>
<strong>Commenter:</strong>
<%= comment.commenter %>
</p>
<p>
<strong>Comment:</strong>
<%= comment.body %>
</p>
<p>
<%= link_to "Destroy Comment", [comment.article, comment], data: {
turbo_method: :delete,
turbo_confirm: "Are you sure?"
} %>
</p>
You need to create a file called app/views/comments/_comment.html.erb so Rails can use it to render each of your comments. This is what you're asking it to do when you write <%= render #article.comments %>. For each element in the collection you passed to render, Rails will try to render a partial, and the path to the partial is derived from the name of the model.
If #article.comments contains 3 records with ids 1, 2, 3, and if
if your app/views/comments/_comment.html.erb contains the following...
<h1>Comment <%= comment.id %></h1>
Then Rails would render this three times, once for each comment, and produce the following:
<h1>Comment 1</h1>
<h1>Comment 2</h1>
<h1>Comment 3</h1>
I am trying to recover the error message of the validations on my form (there must be no duplicate of ingredient) but I always have a MISSING TEMPLATE that I can not correct, I know that It's a routes problem, but I do not know which one.
I have 3 models, Cocktail Ingrédient and Dose, Dose link Cocktail and Ingredient
ERROR MESSAGE
MISSING TEMPLATE
Missing template cocktails/23 with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in: * "/home/dezrt/code/Pseud0/rails-mister-cocktail/app/views"
doses_controller.rb
class DosesController < ApplicationController
before_action :set_dose, only: [:show, :destroy, :edit]
def index
#doses = Dose.all
end
def show
end
def new
#dose = Dose.new
end
def create
#dose = Dose.create(dose_params)
#cocktail = Cocktail.find(params[:cocktail_id].to_i)
#dose.cocktail = #cocktail
if #dose.save
redirect_to cocktail_path(#dose.cocktail)
else
# params[:dose][:cocktail_id] = #cocktail.id.to_s
#ingredient = Ingredient.find(params[:dose][:ingredient_id].to_i)
render cocktail_path(#cocktail)
end
end
def edit
end
def destroy
#dose.destroy
redirect_to cocktail_path(#dose.cocktail)
end
private
def dose_params
params.require(:dose).permit(:cocktail_id, :ingredient_id, :quantity, :description)
end
def set_dose
#dose = Dose.find(params[:id])
end
end
cocktails_contronller.rb
class CocktailsController < ApplicationController
before_action :set_cocktail, only: [:show]
def index
#cocktails = Cocktail.all
end
def show
#cocktail = Cocktail.find(params[:id])
#dose = Dose.new
#dose.cocktail = Cocktail.find(params[:id])
#ingredient = Ingredient.new
#ingredients = Ingredient.all
end
def new
#cocktail = Cocktail.new
end
def create
#cocktail = Cocktail.create(cocktail_params)
if #cocktail.save
redirect_to cocktail_path(#cocktail)
else
#cocktail = Cocktail.new(cocktail_params)
render :new
end
end
private
def cocktail_params
params.require(:cocktail).permit(:name)
end
def set_cocktail
#cocktail = Cocktail.find(params[:id])
end
end
views/cocktails/show.html.erb
<h1>Cocktail X</h1>
<h2>Voici la listes de tout nos cocktails</h2>
<div class="container">
<h3>Nom du cocktail : <%= #cocktail.name %></h3>
<ul>
<h4>Ingredients : <% #cocktail.doses.each do |dose| %></h4>
<li><%= dose.ingredient.name %> : (<%= dose.quantity %><%= dose.description %>)</li>
<%= link_to dose_path(dose), method: :delete do %>
<i class="fa fa-close"></i>
<% end %>
<% end %>
</ul>
</div>
<%= link_to "Retour aux cocktails", cocktails_path %>
<div class="container">
<h4>Ajouter des ingrédients</h4>
<%= simple_form_for [#cocktail, #dose] do |f| %>
<%= f.input :ingredient_id, collection: #ingredients %>
<%= f.input :quantity, label: 'Quantité', error: 'La quantitée est obligatoire' %>
<%= f.input :description, label: 'Description', error: 'La description est obligatoire' %>
<%= f.button :submit %>
<% end %>
</div>
routes.rb
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root "cocktails#index"
resources :cocktails do
resources :doses, only: [:new, :create, :edit]
end
resources :doses, only: :destroy
end
rails routes
Prefix Verb URI Pattern Controller#Action
root GET / cocktails#index
cocktail_doses POST /cocktails/:cocktail_id/doses(.:format) doses#create
new_cocktail_dose GET /cocktails/:cocktail_id/doses/new(.:format) doses#new
edit_cocktail_dose GET /cocktails/:cocktail_id/doses/:id/edit(.:format) doses#edit
cocktails GET /cocktails(.:format) cocktails#index
POST /cocktails(.:format) cocktails#create
new_cocktail GET /cocktails/new(.:format) cocktails#new
edit_cocktail GET /cocktails/:id/edit(.:format) cocktails#edit
cocktail GET /cocktails/:id(.:format) cocktails#show
PATCH /cocktails/:id(.:format) cocktails#update
PUT /cocktails/:id(.:format) cocktails#update
DELETE /cocktails/:id(.:format) cocktails#destroy
dose DELETE /doses/:id(.:format) doses#destroy
Thanks you for your help !
Missing template cocktails/23 with {:locale=>[:en], :formats=>[:html],
:variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby,
:coffee, :jbuilder]}. Searched in: *
"/home/dezrt/code/Pseud0/rails-mister-cocktail/app/views"
This line render cocktail_path(#cocktail) is the reason for the error. render loads a view. So the input to the render normally should be name of the file, in your case show view of the cocktails. Changing it to render 'cocktails/show' should fix the error.
Also you seems to be confused between render and redirect_to. I suggest you to read render vs redirect
I am using carrierwave and remotipart to handle an AJAX form where I upload an album of music and accompanying information to a website. After submitting the form, I would like to redirect the partial where the form was located to the newly created album's page. The form, located in \albums\_new.html.erb looks like this:
<h1>Add a new album</h1>
<div>
<%= form_for(album, remote: true, url: {action: "create"}, html: {multipart: true}) do |f|%>
<h2>Information: </h2>
<div>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :year %>
<%= f.text_field :year %>
<%= f.label :image %>
<%= f.file_field :image %>
</div>
...
<%= f.submit "Add Album" %>
<% end %>
</div>
The controller's new and create methods look like this:
def new
#album = Album.new
#album.producers.build
#album.tracks.build
end
def create
respond_to do |format|
#album = Album.new(album_params)
if #album.save
format.js {redirect_to album_path(#album)}
end
end
end
The redirect currently only works when I submit the form without any file attachments. If I do try to include a file attachment, the upload still succeeds but the redirect fails and I get the following message in my console:
ActionView::MissingTemplate (Missing template albums/show,
application/show with {:locale=>[:en], :formats=>[:html], :variants=>[],
:handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}.
I have been able to implement a workaround where I simply call the same expression in create.js.erb that I do in show.js.erb:
$('#listen-window').html("<%= j (render partial: 'show', locals: {album: #album, tracks: #tracks, producers: #producers}) %>")
and write a create method as such:
def create
respond_to do |format|
#album = Album.new(album_params)
if #album.save
#tracks = #album.tracks
#producers = #album.producers
format.js
end
end
end
Functionally, this code does achieve what I want. However, it does not make sense to me from a routing perspective. How would you write a more elegant solution?
I am new to rails. Currently trying to pull from an api and store the value of each api request into a form for entry into my DB.
My page has a search bar at the top which pulls the form the api and returns the value of each result to the screen with a button prompting to add to the DB.
<ul>
<% #movies.each do |movie| %>
<li><%= movie['Title'] %> | <%= movie['Year'] %> |
<%= form_tag(movies_path, :method => "post") do %> <%=hidden_field :movie, :title, :value => movie["Title"] %> <%= hidden_field :movie, :year, :value => movie["Year"] %> <%= hidden_field :movie, :imdb_id, :value => movie['imdbID'] %> <%= submit_tag "Add Movie", :name => nil %> <% end %> </li><br>
<% end %>
</ul>
My controller for both my create a #movie for the index/ search results page is this
class MoviesController < ApplicationController
def index
if params[:search].present?
#movies = Omdbapi.search(params[:search])
else
render :'/watchlists'
end
end
def show
#movie = Omdbapi.more_info(params[:imdb_id])
end
def create
#movie = Movie.new(movie_params)
if #movie.save
redirect_to movies_path(#movie)
end
end
When I try to hit submit rails throw this error
Missing template movies/create, application/create with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}.
Any help would be awesome.
#movie did not persisted for some reason so that redirect_to hasn't been performed. Add else statement to condition:
if #movie.save
redirect_to movies_path(#movie)
else
render action: 'new'
end
I am implementing ancestry on a nested resource.
resources :loads do
resources :messages
end
Here is my index action
def index
load = Load.find(params[:load_id])
#messages = load.messages.scoped
#message = load.messages.new
end
My index.html.erb is throwing the following error.
Missing partial messages/message with {:locale=>[:en],
:formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in:
* "C:/Sites/final/cloud/app/views"
My index.html.erb is as follow
<% title "Messages" %>
<%= nested_messages #messages.arrange(:order => :created_at) %>
<%= render "form" %>
Here is my nested_message definition
module MessagesHelper
def nested_messages(messages)
messages.map do |message, sub_messages|
render(message) + content_tag(:div, nested_messages(sub_messages), :class => "nested_messages")
end.join.html_safe
end
end
Here is my _message.html.erb
<div class="message">
<div class="created_at"><%= message.created_at.strftime("%B %d, %Y") %></div>
<div class="content">
<%= link_to message.content, message %>
</div>
<div class="actions">
<%= link_to "Reply", new_load_message_url(:parent_id => message) %> |
<%= link_to "Destroy", [message.load, message], :confirm => "Are you sure?", :method => :delete %>
</div>
</div>
Any help appreciated.
This error states that your application has tried to search for a partial _messages.html.erb, as a result of this the partial must not be in your /app/views/messages which results in the message you are being shown. Check your messages directory and check if you have this partial. Going by your nested resources I am guessing your association between Load and Message is:
class Load < ActiveRecord::Base
has_many :messages
end
class Message < ActiveRecord::Base
belongs_to :load
end
Further more I noticed that you have the following line in your index action: #message = load.messages.new surely this does not seem right. Because what your telling your application to do is when the controller recieves a response to render the index action it should also create message by doing #message = load.messages.new which is why it is trying to render the partial.
To clarify things a bit more for you. If in your application you had a link_to to create a new user. Upon clicking the new user it will do something like:
def new
#user = User.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #user }
end
end
And will render `app/views/users/new.html.erb which inside this will most probably have a
<%= form_for #user do |f| %>
<%= render :partial => 'form', :locals => { :f => f } %>
<% end %>
This will call your partial which in normal cases would be _form.html.erb. The create action of a particular controller will come into play when you attempt to save the partial. Usually a create block for a controller will look like this:
def create
#title = "Create a user"
#user = User.new(params[:user])
if #user.save
redirect_to usermanagement_path
flash[:success] = "Created successfully."
else
#title = "Create a user"
render 'new'
end
end
Here inside the create action when your _form.html.erb or _message.html.erb is submitted it will try to create a new user by passing in the user through the params. I do thoroughly believe that your issue may potentially well be:
1) Your missing your _message.html.erb
2) Also you are calling a .new inside your index action.
Hope this clears this up for you.