lundi 18 janvier 2016

Destroy button rails

I would like to create a destroy for menu_items but do not know what and where to put in the information. I think it should be in the menus controller but not sure please help. this is the form i would like to put the button in.

=form_for ["admin", @menu] do |f|
  %p
    =f.text_field :name, class: "form-control"

  %h3 Menu Items
  =f.fields_for :menu_items do |c|
    %p
      =c.text_field :title,placeholder: "Title", class: "form-control"
      =c.text_field :url, placeholder: "enter URL or path here",class: "form-control"
      =c.hidden_field :id






  %p
    =f.submit

  %p
    =link_to "back", admin_menus_path, class: "btn btn-danger"

MenusController:

module Admin
  class MenusController < AdminController
    def index
      @menus = Menu.all
    end

    def new
      @menu = Menu.new
      10.times {@menu.menu_items.build}
    end

    def create
      @menu = Menu.new menu_params
      if @menu.save
        redirect_to admin_menus_path, notice: "Menu created."
      else
        render :new
      end
    end

    def update
      @menu = Menu.find params[:id]
      if @menu.update_attributes menu_params
        redirect_to admin_menus_path, notice: "Menu updated."
      else
        render :edit
      end
    end

    def edit
      @menu = Menu.find params[:id]
      10.times {@menu.menu_items.build}
    end

    def destroy
      @menu = Menu.find params[:id]
      if @menu.destroy
        redirect_to admin_menus_path, notice: "Menu deleted!"
      else
        redirect_to admin_menus_path, alert: "Menu was NOT deleted!!!!"   
      end
    end

    protected
    def menu_params
      params.require(:menu).permit(:name, :menu_items_attributes => [:title, :url, :id])
    end
  end
end

please help!! :(

Aucun commentaire:

Enregistrer un commentaire