Jean Claude Lemens Jean Claude Lemens -3 years ago 76
Ruby Question

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 !

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download