首页 > 解决方案 > ActionView::Template::Error 缺少部分视图

问题描述

需要帮忙!!!!我是 Rails 的新手。我尝试了很多解决方案,但无法解决这个问题。我使用简单形式、Cocoon 和 haml 来处理“has_many :through”关系的嵌套形式(它的多对多关系)。我想保存来自数据库的交易和菜单项。但它不能渲染 _deal_menuitem_fields.html.haml 它说缺少部分视图。

那是我的“_form.html.haml”文件。

.sidenav
= link_to "Dashboard", controller: "dashboard" , :action => "dashboard_manager"
= link_to "Restaurant", controller: "restaurants" , :action => "index"
= link_to "Branch", controller: "branches" , :action => "index"
= link_to "Menu Items", controller: "menuitems" , :action => "index"
= link_to "Deals", controller: "deals" , :action => "index"
= link_to "Orders", controller: "dashboard" , :action => "dashboard_manager"
= link_to "User", controller: "users" , :action => "index"
.logout_sidebar_button
    = link_to "Logout", destroy_user_session_path, method: :delete

.main
= simple_form_for @deal, html: {multipart: true} do |f|
    - if @deal.errors.any?
        #errors
            %p
                #{@deal.errors.count} Prevented this Deal from saving
            %ul
                -@deal.errors.full_messages.each do |msg| 
                    %li = msg
    .panel-body
        =f.association :restaurant, label_method: :restaurant_name, value_method: :id, include_blank: false
        = f.input :deal_name, input_html: {class:'form-control'}
        = f.input :deal_description, input_html: {class:'form-control'}
        = f.input :deal_price, input_html: {class:'form-control'}
        = f.input :deal_rating, input_html: {class:'form-control'}
        = f.input :deal_quantity, input_html: {class:'form-control'}
        = f.input :deal_preptime, hint: "Time Should be in this Format 'HH:MM:SS'" , input_html: {class:'form-control'}
        = f.input :image, input_html: {class:'form-control'}
        =f.simple_fields_for :deal_menuitems do |deal_menuitem|
            = render'deal_menuitem_fields', f: deal_menuitem
        = link_to_add_association 'Add an Item', f, :deal_menuitems
    =f.button :submit, class: "btn btn-primary"

    %br/

= link_to "Back", {:controller => 'deals' ,:action => 'index'}, class: "btn btn-default"

这就是我的 _deal_menuitem_fields.html.haml 文件。

.nested-fields.deal-menuitem-fields
.form-inline
    .menuitem_from_list
        = f.association :menuitem, collection: Menuitem.order(:menuitem_name), prompt: 'Choose an Item', label: false
        = f.input :menuitem_name
    = link_to_add_association 'or create a new Item', f, :menuitem, class: 'add-tag'
    = link_to_remove_association f, class: 'remove-tag btn btn-default btn-xs' do
        .glyphicon.glyphicon-remove

那是我的控制器 deal_controller.rb

class DealsController < ApplicationController
 before_action :find_deal, only: [:show, :edit, :update, :destroy]
 def index
   @deal = Deal.all.order("created_at DESC")
 end
 def show   
 end
 def new
   @deal = Deal.new
 end
 def create
  @deal = Deal.new(deal_params)
  if @deal.save
    redirect_to @deal, notice: "Successfully created New Deal"
  else
    render 'new'
  end
 end
def edit
end

def update
 if  @deal.update(deal_params)
    redirect_to @deal
 else
    render 'edit'
 end     
end

def destroy
  @deal.destroy
  redirect_to :controller => 'deals', :action => 'index', notice:  "Successfully deleted Deal"
end

private

 def deal_params
  params.require(:deal).permit(
  :restaurant_id ,:deal_name, :deal_description, :deal_price, :deal_rating, :deal_quantity, :deal_preptime, :image, 
  deal_menuitems_attributes: [:id , :_destroy ,:menuitem_id, menuitem_attributes: [:id, :_destroy, :menuitem_name]]
  )
 end

 def find_deal  
   @deal = Deal.find(params[:id])
 end
end

那是我控制台的日志..

ActionView::Template::Error (Missing partial deals/_menuitem_fields, application/_menuitem_fields with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder, :haml]}. Searched in:
 * "/home/niazi/Desktop/servemeerepo/ServeMe/app/views"
 * "/var/lib/gems/2.3.0/gems/devise-4.4.3/app/views"):
  2:    .form-inline
  3:        .menuitem_from_list
  4:            = f.input :menuitem
  5:        = link_to_add_association 'or create a new Item', f, :menuitem, class: 'add-tag'
  6:        = link_to_remove_association f, class: 'remove-tag btn btn-default btn-xs' do
  7:            .glyphicon.glyphicon-remove

那是我的模型

class Deal < ApplicationRecord
  #Database RelationShip
    belongs_to :user, optional: true;
   belongs_to :restaurant;
    #belongs_to :branch;

#many to many
   has_many :deal_menuitems , inverse_of: :deal;
   has_many :menuitems, :through => :deal_menuitems, :class_name => 'deal_menuitem';

   has_many :deal_orders;
   has_many :orders , :through => :deal_orders;
   accepts_nested_attributes_for :menuitems;    
   accepts_nested_attributes_for :deal_menuitems, allow_destroy: true
   has_attached_file :image, styles: { medium: "200x200#" }
   validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
 end


class DealMenuitem < ApplicationRecord
  belongs_to :deal;
  belongs_to :menuitem;

  accepts_nested_attributes_for :menuitem, :reject_if => :all_blank
end

class Menuitem < ApplicationRecord
  #Database RelationShip
  belongs_to :user, optional: true;
  belongs_to :restaurant;
  has_one :category;

  #many to many 
  has_many :deal_menuitems, inverse_of: :menuitem;
  has_many :deals, :through => :deal_menuitems;

  has_many :menuitem_orders;
  has_many :orders ,:through => :menuitem_orders;

  has_attached_file :image, styles: { medium: "200x200#" }
  validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end

标签: ruby-on-rails

解决方案


您的部分名称中可能缺少下划线。确保_部分的实际文件名的开头有一个。

例如:

deals/_menuitem_fields.html.haml.

你可能有:

deals/menuitem_fields.html.haml<=== 注意缺少的下划线!!!


推荐阅读