Search code examples
ruby-on-railsruby-on-rails-4archive

Trying to archive a specific feedback in Rails 4


I have created a feedback system and am trying to get a specific feedback to archive. I have tried multiple different configurations, but just can't seem to get it to work. I am not receiving an error, and it looks like it is hitting the controller action as I am receiving the notice. However, the boolean won't change in the DB and the particular feedback will not hide.

Any help would be appreciated!

  • I have added an Archive boolean to my table.
  • I have created a route for archive.
  • I have created the controller action for archive.

Here is my code:

**Controller:**

def archive_feedback
 @listing_feedback = ListingFeedback.find(params[:id])

 respond_to do |format|
   format.html { redirect_to listing_listing_feedbacks_path, notice: "That feedback has been archived." }
   format.json { render :index }
 end
end

**Routes: (My feedback feature is using nested resources)**

resources :listings do
  member do
   get 'like'
   get 'unlike'
   get 'duplicate'
   get 'gallery'
   delete 'gallery' => 'listings#clear_gallery'
   get 'manage_photos'
   get 'craigslist'
   get "add_to_collection"
  end
  resources :listing_feedbacks do
    member do
     put 'archive_feedback'
  end
 end
end

**Index.html.erb:**

<p><%= link_to 'Archive', controller: "listing_feedbacks", action: "archive_feedback", id: listing_feedback.id, archive: :true, method: :put %></p>

Also, how would I get the feedback to hide once it has been archived?


Solution

  • You are not updating @listing_feedback in the archive_feedback method. Change it to below

    def archive_feedback
     @listing_feedback = ListingFeedback.find(params[:id])
     @listing_feedback.update(archive: true)
    
     respond_to do |format|
       format.html { redirect_to listing_listing_feedbacks_path, notice: "That feedback has been archived." }
       format.json { render :index }
     end
    end