Cannot make destroy comments work (RAILS) - ruby-on-rails

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

Related

Missing partial comments/_comment with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder] } [closed]

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>

"Template missing error " in rails [duplicate]

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.

Missing template Rails version: 5.0.5

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

Rails 4: How can I use "redirect_to" with AJAX to render a partial after uploading file(s)?

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?

Form_helper and routing

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

Resources