John Cowan
John Cowan

Reputation: 1674

Rails Confirm Delete Popup

I have a slight twist on the Are you sure you want to delete this? confirm popup.

ruby-2.3.7 Rails 4.2.11.1

I also have the jquery-rails gem.

When a certain link is clicked, I need to delete only about four fields in a record, not the entire record. That is working, but what I'd like to do is use a confirm pop up window when the link is clicked: Are you sure...?

When the link is clicked I send the user to a delete_interview_results action in the controller, not a destroy or delete action.

I can create a view, redirect users there to have them confirm, but that doesn't seem like the best UI experience.

View:

<%= link_to 'Delete Interview Results',
             delete_interview_results_manage_applicant_path(@applicant), 
             class: 'btn',
             confirm: "Are you sure?" %>

Controller:

def delete_interview_results
    @applicant = Applicant.find(params[:id])
    redirect_to edit_manage_applicant_path(@applicant), notice: "You just deleted the interview results for #{@applicant.full_name}."
end

Routes:

resources :applicants, only: [ :edit, :update ] do
  ...
  get :delete_interview_results
  ...
end

I do not see any confirm popup window. I thought having the jquery-rails gem did this.

When I add a method: :delete to this I get an error:

No route matches [DELETE] "/manage/applicants/139289/delete_interview_results"

Hopefully, I'm just missing a small piece. I figure there has to be a way to do this.

Thanks for any tips n tricks.

Upvotes: 0

Views: 1997

Answers (1)

John Cowan
John Cowan

Reputation: 1674

This worked: data: { confirm: "Are you sure?" }

Found the answer here on StackOverflow: rails 3, how add a simple confirmation dialog when user clicks a link

It says rails3, but it worked for me in Rails 4.2.11

<%= link_to 'Delete Interview Results',
             delete_interview_results_manage_applicant_path(@applicant), 
             class: 'btn',
             data: { confirm: "Are you sure?" } %>

Upvotes: 2

Related Questions