【问题标题】:Missing template Rails version: 5.0.5缺少模板 Rails 版本:5.0.5
【发布时间】:2017-08-13 12:14:40
【问题描述】:

我正在尝试在我的表单上恢复验证的错误消息(必须没有重复的成分)但我总是有一个我无法更正的 MISSING TEMPLATE,我知道这是一个路线问题,但我做到了不知道是哪一个。 我有 3 个模型,鸡尾酒成分和剂量,剂量链接鸡尾酒和成分

错误信息

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

铁路路线

            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

感谢您的帮助!

【问题讨论】:

  • 什么时候出现这个错误?
  • 当我想添加已存在于鸡尾酒配料列表中的配料时。我添加了一个验证来禁止这种情况,所以我无法在我的表单上收到错误消息

标签: ruby-on-rails ruby routes


【解决方案1】:

缺少带有 {:locale=>[:en]、:formats=>[:html] 的模板鸡尾酒/23, :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :咖啡,:jbuilder]}。搜索:* "/home/dezrt/code/Pseud0/rails-mister-cocktail/app/views"

这行render cocktail_path(@cocktail) 是错误的原因。 render 加载视图。所以render 的输入通常应该是文件名,在你的情况下showcocktails 的视图。将其更改为 render 'cocktails/show' 应该可以修复错误。

另外,您似乎对renderredirect_to 感到困惑。我建议你阅读render vs redirect

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-26
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 2016-11-24
    • 1970-01-01
    相关资源
    最近更新 更多