ActionView :: Template :: Error缺少部分视图

时间:2018-08-14 17:15:59

标签: ruby-on-rails

需要帮助!!!我是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

那是我的控制器Deals_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

2 个答案:

答案 0 :(得分:0)

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

E.G:

deals/_menuitem_fields.html.haml

您可能有:

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

答案 1 :(得分:0)

第一个解决方法:

根据错误状态,您缺少app/view/deals/_menuitem_fields.html.haml(错误地拥有_deals_menuitem_fields.html.haml)。因此,您需要在_menuitem_fields.html.haml目录中创建app/views/deals