catmal
catmal

Reputation: 1758

Rails delete record - param is missing or the value is empty

My expense_categories controller:

 def delete
       @expense_category = ExpenseCategory.find(params[:id])
    end

  def destroy
     expense_category = ExpenseCategory.find(params[:id]).destroy
     flash[:notice] = "ExpenseCategory '#{expense_category.description}' destroyed successfully."
     redirect_to(expense_categories_path)
    end

My delete view:

<%= form_for @expense_category do |f| %>
<%= submit_tag("Delete Expense Category", class:"button alert") %>

My expense_categories table:

class CreateExpenseCategories < ActiveRecord::Migration[5.1]
  def change
    create_table :expense_categories do |t|
      t.string :description

      t.timestamps
    end
  end
end

My routes:

  resources :expenses
  resources :expense_categories

  get ":controller/:action/:id"

Whenever I try to delete a record I get

ActionController::ParameterMissing in ExpenseCategoriesController#update

param is missing or the value is empty: expense_category

On page error parameters are

{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"dWPl0kJaoogDIk1vnTjK8+vvmf7MbI+lzpuRdQ0d6sBZJ+3eFZBk1GdOf+su46d+129niW9wH91YPi9z7PKmaQ==", "commit"=>"Delete Expense Category", "id"=>"1"}

Why is expense_category empty?

Upvotes: 0

Views: 462

Answers (1)

Pavan
Pavan

Reputation: 33542

ActionController::ParameterMissing in ExpenseCategoriesController#update

param is missing or the value is empty: expense_category

The request is sent to update action instead of destroy and resulted in that error. You need to specify method: :delete to send the request to destroy method.

<%= form_for @expense_category, method: :delete do |f| %>

Upvotes: 2

Related Questions