I am fairly new to Rails and working on building my first app outside of courses like CodeAcademy. I am running into issues with Parameters + building a delete function. I can display all tasks on an index when I make the private tasks_params record look like this:
private
def task_params
params.require(:task).permit(:name, :description, :due_date)
end
Clicking on the delete function and confirming it throws this:
param is missing or the value is empty: task
When I no longer "require" the :task param and instead "permit" it, like so:
private
def task_params
params.permit(:task, :name, :description, :due_date)
end
My app returns:
Couldn't find Task with 'id'=
Below are the routes.rb file, Tasks_Controller.rb file, and index.html.erb files. Any help will be much appreciated.
routes.rb
get 'signup' => 'users#new'
resources :users
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
get 'tasks' => 'tasks#index'
get 'tasks/new' => 'tasks#new'
post 'tasks' => 'tasks#create'
delete 'tasks/:id' => 'tasks#delete'
get 'tasks/:id/edit' => 'tasks#edit'
put 'tasks/:id' => 'tasks#update'
end
Task_Controller.rb class TasksController < ApplicationController def index @task = Task.last(100) end
def new
@task = Task.new
end
def create
@task = Task.new(task_params)
if @task.save
redirect_to '/tasks'
else
render 'new'
end
end
def edit
@task = Task.find(task_params[:id])
end
def update
@task = Task.find(task_params[:id])
@task.update_attributes(task_params[:description])
redirect_to '/tasks'
end
def delete
@task = Task.find(task_params[:id])
@task.delete
redirect_to tasks_path
end
private
def task_params
params.permit(:task, :name, :description, :due_date)
end
end
index.html.erb
<h2>Your Tasks</h2>
<div class="tasks">
<div class="container">
<% @task.each do |task| %>
<div class="task">
<p>Title: <%= task.name %></p>
<p>Description: <%= task.description %></p>
<p>Due Date: <%= task.due_date %></p>
<%= link_to 'delete', task_path(task),
method: :delete,
data: { confirm: 'Are you sure?' } %>
</div>
<% end %>
</div>
</div>
Thanks in advance!