Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6708817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:48:20+00:00 2026-05-26T07:48:20+00:00

I’ve some difficulty in determining any way to update my objects as a group.

  • 0

I’ve some difficulty in determining any way to update my objects as a group. I have an Alert model which has some alerts for the user. I’d like to provide a way to update them in bulk, as I’ve noticed that I often want to. I just don’t know Rails well enough to know what ‘the Rails way’ would be.

My controller:

class AlertsController < ApplicationController
  def index
    @alerts = Alert.all(show: true)
  end
  def destroy
    alert = Alert.get(params[:id])
    ## I lied, I'm not really deleting things, just hiding them from showing
    alert.update(show: false) if alert 
    respond_to do |format|
      format.js { render :nothing => true }
    end
  end

I’ve been using this code in my *.html.haml view to create the delete link for each individual alert:

  - @alerts.each do |alert|
      %span{ :class => 'description' }=alert.description
      = link_to "Destroy", alert, :confirm => "Are you sure", :method => :delete, :remote => :true

What I want to have is a link on my index page Delete All which I could use to hide(update) all of the alerts. I’ve been attempting this with a destroy_all method for my controller, but I felt like it wasn’t quite the right approach, as I didn’t know how to link to something like that. Thank you for pointing me in the right direction.

Here is the destroy_all method I’d hacked together, for reference.

  def destroy_all
    @alerts.each do |alert|
      alert.update(show: false) if alert
      respond_to do |format|
        format.js {render :nothing => true}
      end
    end
  end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:48:20+00:00Added an answer on May 26, 2026 at 7:48 am

    I don’t know how you have routed to this controller but I would probably do something like this:

    #routes.rb
    resources :alerts do
      delete :remove_all, :on => :collection
    end
    

    This will create a route looking like /alerts/remove_all which can only be called through the DELETE protocol. By specifying :collection it tells the routing the this route is not a sub route to one specific Alert so no :id is included.

    Then the controller action could probably look something like this:

    def remove_all
      Alert.update_all({ show: false }, { show: true })
      respond_to do |format|
        format.js {render :nothing => true}
      end
    end
    

    The update_all function will make sure that there is only one database update instead of one for each alert that is to be removed. The first argument is a hash containing the updates and the second argument is a hash with the conditions for which records are to be updated.

    And from the view you should be able to link to the action like this:

    = link_to "Remove All", remove_all_alerts_path, :confirm => "Are you sure", :method => :delete, :remote => :true
    

    remove_all_alerts_path is a path helper that is automatically generated when you use the above syntax in routes.rb

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.