2017-08-13 12 views
0

私のフォームのバリデーションのエラーメッセージを修復しようとしていますが(コンポーネントの重複はありません)、私はいつも修正できないテンプレートがあります。それはルートの問題ですが、私はどちらが問題か分かりません。 私が3つのモデル、カクテル成分および線量、線量リンクのカクテルと成分テンプレートがありませんRailsのバージョン: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 /レール・ミスター・カクテル/アプリ/ビュー"

この行render cocktail_path(@cocktail)は、エラーの理由です。 renderはビューをロードします。したがって、renderへの入力は通常、あなたのケースではcocktailsshowビューのファイルの名前にする必要があります。 render 'cocktails/show'に変更するとエラーが修正されます。

また、renderredirect_toの間で混乱しているようです。私はあなたが読むことをお勧めしますrender vs redirect

+1

はい、私のフォームによく戻ってくれてありがとうございます;-) –

関連する問題