Search code examples
ruby-on-railscontrollerroutesdestroy

ActionController::ParameterMissing in TagsController#create


I am attempting to trigger a destroy action from an index page for tags attached to multiple records. However, when the action is triggered I get the above error in the create action. The error does not occur when the create action is invoked. My code is as seen below.

Tag Controller

class TagsController < ApplicationController
before_action :require_user, only: [:edit, :update, :destroy]
before_action :set_search

def new
    @tag = Tag.new
end

def create
    tag = Tag.create(tag_params)
    if tag.save
        redirect_to tags_path
    else
        redirect_to sign_up_path
    end
end

def destroy
    @tag = Tag.find(params[:tag_id])
    @tag.destroy

    redirect_to tags_path
end

private
    def tag_params
        params.require(:tag).permit(:name)
    end
end

Routes

Rails.application.routes.draw do
  # For details on the DSL available within this file, see 
http://guides.rubyonrails.org/routing.html
  resources :recipes do
    resources :ingredients, :steps
    put :favorite, on: :member
  end

  resources :users

  get 'recipes' => 'recipes#index'
  get 'recipes/:id' => 'recipes#show'

  get 'signup' => 'users#new'

  get 'tags' => 'tags#index'
  get 'new_tags' => 'tags#new'
  post 'tags' => 'tags#create'
  delete 'tags' => 'tags#destroy'

  get 'login' => 'sessions#new'
  post 'login' => 'sessions#create'
  delete 'logout' => 'sessions#destroy'

  root 'recipes#index'

end

Index

  <%= link_to 'New Tag', new_tags_path(@tag) %> 

  <% Tag.find_each do |tag| %>
    <%= tag.name %>
    <%= link_to 'Delete Tag', @tag,
              method: :destroy,
              data: { confirm: 'Are you sure?' } %>
  <% end %>

Solution

  • The error stemmed from a combination of issues in the Link To statement in the view. The Link as it should be written is below...

    <%= link_to 'Delete Tag', tag_path(tag),
              method: :delete,
              data: { confirm: 'Are you sure?' } %>