For some reason the show link throws error while trying to render its partial, even though the partial exists (named _show.html.erb)
Made no changes to the routes file. It simply has the root declaration and resources :notes
And I have not deleted the original "show.html.erb" generated by Scaffold.(matters?)
index.html.erb
<% #notes.each do |note| %>
<% note.title %>
<%= link_to 'Show', note, remote: true, class: "note-show" %>
<% end %>
<div id="note-show">
</div>
_show.html.erb
<% note.title %>
<% note.body %>
show.js.erb
$('#note-show').html("<%= render :partial => 'show' %>");
notes controller
class NotesController < ApplicationController
before_action :set_note, only: [:show, :edit, :update, :destroy]
def index
#notes = Note.all.order('created_at DESC')
end
def show
respond_to do |format|
format.js
format.html
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_note
#note = Note.find(params[:id])
end
Error is
ActionView::MissingTemplate in Notes#show
Showing /home/arjun/rails/notes/app/views/notes/show.html.erb where line # raised:
Missing partial notes/_show, application/_show with {:locale=>[:en], :formats=>[:js, :html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
* "/home/arjun/rails/notes/app/views"
* "/home/arjun/.rvm/gems/ruby-1.9.1-p431/gems/twitter-bootstrap-rails-3.2.0/app/views"
Strangely on some links it simply shows
$('#note-show').html("hello
");
I found this by using the Browser Page Source
What is incorrect here?
In show.js.erb you have to escape javascript, (escape_javascript or j)
$('#note-show').html("<%= j render 'show' %>");
Related
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.
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 a student and am working on an app, however i am getting this error
Missing template jobs/index, application/index with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "
Im not sure how to decipher this, I started getting this when i created a show page, can someone offer some suggestions?
Show page:
<H1>Jobs Assigned to <%= #boat.name %></H1>
<% #boat.name.each do |job| %>
<h3>Cargo Origin: <%= job.cargo_origin %></h3>
<h3>Destination: <%= job.destination %></h3>
<h3>Containers Needed: <%= job.containers_needed %></h3>
<h3>Cargo: <%= job.cargo %></h3>
<h3>Job Cost: <%= job.job_cost %></h3>
<% end %>
Jobs Controller:
class JobsController < ApplicationController
def index
#jobs = Job.all
end
def show
#job = Job.find(params[:id])
redirect_to user_path
end
def new
#boat = Boat.find(params[:boat_id])
#job = Job.new
end
def create
#boat = Boat.find(params[:boat_id])
#job = #boat.jobs.build(job_params)
#job.save
redirect_to user_path (current_user)
end
def edit
end
def update
end
def destroy
end
private
def job_params
params.require(:job).permit(:cargo_origin,:destination,:containers_needed,:cargo,:job_cost).merge(user_id: current_user.id)
end
end
You should check existence of app/views/jobs/index.html.erb view. This view is being used when you visit /jobs path and being rendered by JobsController#index action.
This error come due to non existence of views.
Create relevant views related o controllers and actions
For Controller Jobs and action index
Create app/views/jobs/index.html.erb
Like this create other views
So this is my first experience with Rails, I did everything according to http://guides.rubyonrails.org/getting_started.html#generating-a-model
And now i can't destroy a commment... I tried several google search, i tried to remove the file then generate it again, nothing works... My comments_controller.rb
class CommentsController < ApplicationController
def index
end
def show
end
def new
end
def edit
end
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)
end
private
def comment_params
params.require(:comment).permit(:commenter, :body)
end
end
p>
<strong>Commenter:</strong>
<%= comment.commenter %>
</p>
And my _comment.html.erb is
<p>
<strong>Comment:</strong>
<%= comment.body %>
</p>
<p>
<%= link_to 'Delete the comment', [comment.article, comment],
method: :delete,
data: { confirm: 'Are you sure?' } %>
</p>
I receive this when i click on the "Delete the comment" button
Missing template comments/show, application/show with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/root/blog/app/views"
But that shouldn't be the problem since I'm doing a redirect to the article path ? Right??
I sure hope this is a typo somewhere but now I can't find it....
Edit : this is the text of the button
<form action="/articles/2/comments/1" class="button_to" method="post"> <div><input type="submit" value="Delete the comment" data="{:confirm=>"Are you sure?"}" method="delete"><input name="authenticity_token" type="hidden" value="rW+wymZlJp2cMrhGUX31Y/vLz+qZ1VHTEVHaavS7YTU="></div></form>
Looks like the method :delete of the link is not working.
Make sure you have javascript turned on and add
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
to the <head> of your layout/application
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