Confirm boxes not working for delete links only - ruby-on-rails

I am currently banging my head against the desk trying to figure out why this is not working. I am trying to get the ujs confirmation box to show up when a delete link is clicked. Currently, the item is deleted with no confirmation box. Here is my delete link:
<%= link_to "void", project, method: :delete, data: {comfirm: "Are you sure you want to delete this project?"} %>
Here is where it gets strange. The following link (not a delete link) works as expected:
<%= link_to "About", about_path, data: {confirm: "test test"} %>
I did some digging in the gem itself and was able to discover that, with the delete link, the data-message attribute is not being parsed correctly in the following code. Specifically, the
if (!message) { return true; }
is returning true, where message is defined as follows:
message = element.data('confirm')
Note: element is the entire link itself. Can anyone help me find out why this is happening? I am using Rails 3.2 if it helps.

I have done this, just make it confirm instead of comfirm, and it works fine.
<%= link_to "void", project,method: :delete, data: {confirm: "Are you sure you want to delete this project?"} %>

Related

Change the confirm modal in Rails 6 link_to

Is there a way to change the { confirm: 'Are you sure you want to report this challenge?' } alert thing, like a modal in the middle of the screen with an input where I can write something?
<%= link_to(report_post_path(#post), method: :post, data: { confirm: 'Are you sure you want to report this post?' }) do %>
Or I have remove the data: { confirm: 'Are you sure you want to report this post?' } and make the modal myself?
you can try gem data-confirm-modal, it is have good documentation.
Also you can check any solution from gists, I think it will be good gist.
Third option to search other solutions on google with request rails confirmation modal. But for me the gem the best solution

Issue with delete action

I have a problem with my delete action and link. Here is my link :
<%= link_to 'Supprimer la thématique', #theme, method: :delete, data: { confirm:'Êtes-vous sûr de vouloir supprimer la thématique' }, :class => "btn btn-default" %>
And I don't know why, but this link redirect to the show action of my model 'theme'.
I can resolve this by using button_to but with a button_to I'm not able to set a data confirm :
<%= button_to "Supprimer la thématique", #theme, :method=>:delete, data: { confirm: 'Êtes-vous sûr de vouloir supprimer la thématique ?'}, :class=> 'btn btn-default' %>
Do you have any ideas why the link to doesn't redirect to the delete action or why I can't have a data confirm for my button_to ?
Clicking a link always sends a GET request, unless there's Javascript in place that does something different, such as sending an Ajax request or submitting a hidden form. When using regular resourceful routes, the show action and destroy action share a route, and determine what to do based on the method (as you probably know).
The button_to method should support a confirmation dialog; if it doesn't work, then there's probably some Javascript missing.
Both your problems point to a missing unobtrusive Javascript driver. If using jquery-rails (the standard option), make sure that it's listed in your Gemfile:
gem 'jquery-rails'
and that you're loading it in your application.js:
//= require jquery
//= require jquery_ujs
Also be sure that you're calling the javascript_include_tag method in your application layout. Once you've got jquery-rails loaded properly, you should be able to get the results you're looking for with either method you've attempted.
For more information on loading jquery-rails, see the Github repo.

Hide link in rails based on condition

I have an Opportunity and a User model. A User can log in as an admin (I have admin as a boolean attribute). I want users to be able to delete opportunities if and only if they are an admin and I was wondering if anyone had any idea how to do this? So far I have the following delete link for my opportunity:
views/opportunities_opportunity
<%= link_to_if(#user.admin?, "Delete", opportunity, method: :delete, data: {confirm: 'Are you sure?'}) %>
However, I keep getting the error "undefined method `admin?' for nil:NilClass"
Please help. Thanks!!
fixed... I use #current_user.admin? instead and it worked.

How to make confirm message i18n compatible

I'm relatively new to Rails, and have been programming a few months.
I'm trying to use the t() method for internationalization, but it doesn't seem to work when I ask for confirmation in a link_to.
For example, when I write
<%= link_to t( ".delete_student_info"),
#student,
method: :delete,
confirm: "child_deletion_confirmation"
%>
...I predictably get a link_to that works and asks the confirmation question
However, when I write
<%= link_to t( ".delete_student_info"),
#student,
method: :delete,
confirm: t( ".child_deletion_confirmation")
%>
...I get the following output
Child Deletion Confirmation" data-method="delete" href="/en/student_profiles/41" rel="nofollow">Delete Student Info
Is there something conceptual that I am missing? I've looked in the Rails Guides Rails i18n API, but it doesn't address this issue. I'm thinking that maybe the confirm: is something different, but I don't know how to look it up. Any ideas?
I tried this out on my Rails 4 console and it worked fine:
helper.link_to "Visit Other Site", "http://www.rubyonrails.org/", data: { confirm: I18n.t("date.formats.default") }
# => "<a data-confirm=\"%Y-%m-%d\" href=\"http://www.rubyonrails.org/\">Visit Other Site</a>"`.
Now, note the behavior when using nil for the :confirm is like what you're seeing:
helper.link_to "Visit Other Site", "http://www.rubyonrails.org/", data: { confirm: nil }
# => "Visit Other Site"
So this makes me think that somehow your translation is evaluating to nil. However, I can't seem to figure out how to duplicate that issue...
I'll expand this answer to try to help more if you can show what the translations file looks like?

How to setup delete selected messages feature in ruby on rails

I'm about to finish the final part of my thread messaging system for users. All deletion works great however before I move on to my next feature I'd like to give users the ability to delete selected messages.
Here's a way I've thought of doing it so far.
Add a check box tag to the each loop that loops through each message.
Have a "delete selected" link that goes to my messages controller "destroy_selected_messages" action.
What I need to do is some how grab an array of all the selected messages id's. Then pass it to the path as an argument. The delete all links path.
<%= link_to 'Delete Selected', messages_destroy_selected_messages_path(ARRAY_WITH_IDS), :method => :delete, :confirm => "Are you sure?" if #current_thread_messages.any? %>
This delete selected link won't be part of the loop because I don't want it showing for every message but at the top of the thread instead.
I need to figure out how to pass the array with all the selected messages ideas into that argument. How do I get them from the each loop without going into my messages helper and writing some funky method.?
I have the checkbox tag e.g. check_box_tag ... how do I setup an empty array and then so I can pass in the messages id? e.g.:
<%= check_box_tag ......., :value => message.id &>
Help would be appreciated. I looked at an old screencast in railscasts but it's from 2007 I think.
Kind regards
You can make name="message_ids[]" for multi select inputs. It will get passed as an array through HTTP server to your params[:message_ids].
From the HTML side of the problem, I think that form helper <%= check_box_tag "message_ids[]", :value => message.id %> should suffice.
In the controller action, log the params[:message_ids] and look it up, it should be an Array.

Resources