Delete all the contents of the list page [Ruby on Rails]

Delete all selected contents from the content list page

Set a checkbox for each post (View)

Checkbox is set for each content post Since Ajax is not used, XML communication is turned off with the form_with option.

ruby:index.html.erb


<%= form_with model: @contents, url: contents_path, method: :delete, local: true do |f| %>
#local:Turn off XML communication with true
    <% @contents.each do |content| %>
        <% @count += 1%>
        <td>
            <%= f.check_box :content_ids, {type: 'checkbox', class: 'checkbox-select', id: "checkbox#{@count}", multiple: true}, content.id %>
             <%= f.label :'', for: "checkbox#{@count}",class: 'select-label' %>
        </td>   
   <% end %>
<% end %>

This will add a checkbox for each post

I tried to summarize only the necessary options of form_with according to the purpose of this time

python


 <%= f.check_box :content_ids, {type: 'checkbox' multiple: true}, content.id %>

multiple: Add true to allow multiple values to be sent to the controller as an array.

A hash called contents is sent to the controller. key is contents_ids, value is an array. And the content.id is in the array

Value sent from view


"content"=>{"content_ids"=>["1", "2"]}}

It's complicated (laughs)

Implement the value acquisition / deletion function in the controller (controller)

First, get the value sent from View with the strong parameter

contents_controller.rb


def select_content_params
    ids = params.require(:content).permit(content_ids: [])
    ids.values[0]
end

1st line: Only content_ids in the sent content can be used, and if unexpected data is sent, it will be ignored. 2nd line: Extract only the array using the values method for ids. Since the value at this time is [["1", "2"]], specify [0] and extract only ["1", "2"].

After that, write the process using the formatted value

contents_controller.rb


def select_destroy
  if select_content_params.uniq.count == 1
    redirect_to contents_url, notice: "Please select the item to delete"
  else
    select = []
    select_content_params.map{|n|
      select << n
      select.delete("0")
    }
      select.each{|id|
        content = Content.find(id)
        content.destroy
      }
    redirect_to contents_url, notice: "I deleted the bookmark"
  end
end

Strictly speaking, values that are not checked in the check box are sent as 0, so the uniq method is used to eliminate duplication, and the count method is used to count the number of array values and if branch. Not checked → Only 0 == 1 Checked → 0 and id exist> 1

If it is not checked, it may be easier to return nil, but please do as you like.

Luding settings

routes.rb


delete "contents" => "contents#select_destroy", as: 'select_destroy'

Completed with!

Thank you for your cooperation. If you have any questions, I would be grateful if you could ask for a professor!

Recommended Posts

Delete all the contents of the list page [Ruby on Rails]
part of the syntax of ruby ​​on rails
[Ruby on Rails] Until the introduction of RSpec
Determine the current page with Ruby on Rails
[Ruby On Rails] How to search the contents of params using include?
Docker the development environment of Ruby on Rails project
Try using the query attribute of Ruby on Rails
[Java] Delete the elements of List
[Ruby on Rails] Quickly display the page title in the browser
A note about the seed function of Ruby on Rails
[Ruby on Rails] Confirmation page creation
[Ruby] Display the contents of variables
[Ruby on Rails] Change the save destination of gem refile * Note
Explanation of Ruby on rails for beginners ⑤ ~ Edit and delete database ~
[Ruby on Rails] Introduction of initial data
[Rails] Addition of Ruby On Rails comment function
Let's summarize "MVC" of Ruby on Rails
[Ruby] Cut off the contents of twitter-ads
Ruby on Rails controller create / delete command
[Ruby on Rails] Japanese notation of errors
Explanation of Ruby on rails for beginners ①
[Ruby on rails] Implementation of like function
How to solve the local environment construction of Ruby on Rails (MAC)!
Ruby on Rails When you don't know the cause of rollback when saving.
Implementation of Ruby on Rails login function (Session)
[Rails] Button to return to the top of the page
[Android] List all setting items on the setting screen
When the Ruby on Rails terminal rolls back
Publish the app made with ruby on rails
Ruby on Rails ~ Basics of MVC and Router ~
[Ruby on Rails] A memorandum of layout templates
[Ruby on Rails] Post editing function (update, delete)
[Ruby on Rails] Individual display of error messages
[Ruby on Rails] How to make the link destination part of the specified id
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
[Ruby on Rails] Implement a pie chart that specifies the percentage of colors
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
Ruby on Rails <2021> Implementation of simple login function (form_with)
[Ruby on Rails] Asynchronous communication of posting function, ajax
How to delete / update the list field of OneToMany
List the contents of categories created with Active Hash
[Ruby on Rails Tutorial] Error in the test in Chapter 3
Implementation of Ruby on Rails login function (devise edition)
[Ruby on Rails] How to change the column name
[Ruby on Rails] Implementation of tagging function/tag filtering function
Solve the N + 1 problem with Ruby on Rails: acts-as-taggable-on
[Rails] How to get the contents of strong parameters
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
Explanation of Ruby on rails for beginners ② ~ Creating links ~
(Ruby on Rails6) Reflecting the posted content from the form
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
[Ruby on Rails] Implementation of validation that works only when the conditions are met
[Ruby On Rails] Description that allows only specific users to transition to the edit page
[Rails] Put the same restrictions on multiple pieces of information
[Ruby On Rails] How to search and save the data of the parent table from the child table
Ruby on rails learning record -2020.10.03
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
Ruby on rails learning record -2020.10.04