2017-08-13 81 views
0

我想恢復我的表單上的驗證錯誤消息(必須沒有重複的成分),但我總是有一個MISSING TEMPLATE,我不能糾正,我知道這是一個路線問題,但我不知道哪一個。 我有3種型號,雞尾酒成分和劑量,劑量鏈接雞尾酒和成分Missing template Rails version:5.0.5

錯誤信息

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 

意見/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 

軌道路線

  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 

感謝您的幫助!

+0

你什麼時候有這個錯誤? – Pavan

+0

當我想添加雞尾酒成分列表中已經存在的成分時。我添加了一個驗證來禁止這種情況,所以我無法在我的表單上獲得錯誤消息 –

回答

0

缺少模板雞尾酒/ 23含{:區域設置=> [:EN]:格式=> [:HTML], :變體=> [],:處理器=> [:生的,:ERB, :html,:builder,:ruby, :coffee,:jbuilder]}。搜查:* 「/家/ dezrt /代碼/ Pseud0/Rails的老總雞尾酒/應用/視圖」

此行render cocktail_path(@cocktail)是錯誤的原因。 render加載視圖。所以render的輸入通常應該是文件的名稱,在您的案例show視圖cocktails。將其更改爲render 'cocktails/show'應修復該錯誤。

此外,你似乎在renderredirect_to之間感到困惑。我建議你閱讀render vs redirect

+1

是的,謝謝我在我的表單上很好地返回;-) –