Need Help!!!! I am new to Rails. I tried many solution but could not able to solve this issue. I used Simple form ,Cocoon and haml to handle nested form for "has_many :through" relation( Its many to many relation). I want to save deals and menuitems that come from the db. but it can't render _deal_menuitem_fields.html.haml It says partial view is missing.
Thats my '_form.html.haml' file.
.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"
And thats my _deal_menuitem_fields.html.haml file.
.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
That's my controller 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
Thats log from my console..
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
thats My models
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
It may be a missing underscore in your partial name. Ensure there's a _
at the beginning of your partial's actual file name.
EG:
deals/_menuitem_fields.html.haml
.
You may have:
deals/menuitem_fields.html.haml
<=== Note the missing underscore!!!
First fix:
As the error states, you are missing app/view/deals/_menuitem_fields.html.haml
(you mistakenly have _deals_menuitem_fields.html.haml
). So, you'll need to create the _menuitem_fields.html.haml
in the app/views/deals
directory.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.