link_to_remove_association
works for view but doesn't not delete in the database. Not sure what's wrong in my code. I can add and update totally fine.
Parameters:
"todo_list" => {
"name" => "Stuff to do",
"todo_tasks_attributes" => {
"0" => {
"_destroy" => "1",
"name" => "1",
"completed" => "0",
"due" => "",
"id"=>"3"
},
"1" => {
"_destroy" => "false",
"name" => "2",
"completed" => "0",
"due" => "",
"id" => "4"
},
"2" => {
"_destroy" => "false",
"name" => "3",
"completed" => "1",
"due" => "",
"id" => "5"
},
"3" => {
"_destroy" => "false",
"name"=>"4",
"completed" => "0",
"due" => "",
"id" => "6"
}
}
},
"commit" => "Update Todo list", "id" => "2" }
In my log I get:
Unpermitted parameter: :_destroy
Unpermitted parameter: :_destroy
Unpermitted parameter: :_destroy
Unpermitted parameter: :_destroy
This is my TodoList controller
class TodoListsController < ApplicationController
before_action :set_todo_list, only: [:show, :edit, :update, :destroy]
# GET /todo_lists
# GET /todo_lists.json
def index
@todo_lists = TodoList.all
end
# GET /todo_lists/1
# GET /todo_lists/1.json
def show
end
# GET /todo_lists/new
def new
@todo_list = TodoList.new
end
# GET /todo_lists/1/edit
def edit
end
# POST /todo_lists
# POST /todo_lists.json
def create
@todo_list = TodoList.new(todo_list_params)
respond_to do |format|
if @todo_list.save
format.html { redirect_to @todo_list, notice: 'Todo list was successfully created.' }
format.json { render :show, status: :created, location: @todo_list }
else
format.html { render :new }
format.json { render json: @todo_list.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /todo_lists/1
# PATCH/PUT /todo_lists/1.json
def update
if @todo_list.update(todo_list_params)
redirect_to edit_todo_list_path(@todo_list), notice: 'Todo list was successfully updated.'
else
render :edit
end
end
# DELETE /todo_lists/1
# DELETE /todo_lists/1.json
def destroy
@todo_list.destroy
respond_to do |format|
format.html { redirect_to todo_lists_url, notice: 'Todo list was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_todo_list
@todo_list = TodoList.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def todo_list_params
# params.require(:todo_list).permit(:name, todo_tasks_attributes: [:id, :_destory, :todo_list_id, :name, :completed, :due])
params
.require(:todo_list)
.permit(:name, todo_tasks_attributes: TodoTask.attribute_names.map(&:to_sym).push(:_destory))
end
end
TodoList model
class TodoList < ApplicationRecord
has_many :todo_tasks, dependent: :destroy
accepts_nested_attributes_for :todo_tasks,
allow_destroy: true,
reject_if: proc { |att| att['name'].blank? }
end
todotask model
class TodoTask < ApplicationRecord
belongs_to :todo_list, optional: true
end
_form
<%= simple_form_for(@todo_list) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name %>
</div>
<table class='table'>
<thead>
<tr>
<th></th>
<th>Task Name</th>
<th>Completed</th>
<th>Due</th>
</tr>
</thead>
<tbody class='todo_tasks'>
<%= f.simple_fields_for :todo_tasks do |builder| %>
<%= render 'todo_task_fields', f: builder %>
<% end %>
</tbody>
</table>
<div class="form-actions">
<%= f.button :submit %>
<div class="links">
<%= link_to_add_association('Add Task', f, :todo_tasks, class: 'btn btn-primary', data: { association_insertion_node: '.todo_tasks', association_insertion_method: :append } ) %>
</div>
</div>
_todo_task_fields
<tr class="nested-fields">
<td>
<%= link_to_remove_association "remove task", f, class: 'btn btn-default btn-xs' %>
</td>
<td><%= f.input :name, label: false %></td>
<td><%= f.input :completed, label: false %></td>
<td><%= f.input :due, label: false, as: :string %></td>
</tr>
Sily me... I had a spelling mistake on my params :_destory -> :_destroy.
Problem solved