With Rails I love how you can just do,
link_to "Click", some_path, :data => {:confirm => "Are you sure?"}.
Is there a simple way to pop up an alert dialog instead? Could I try something like,
link_to "Click", some_path, :data => {:ALERT => "Are you sure?"}?
Rails only supports :confirm out of the box, because it's convenient for delete links. All it really does is add "data-confirm" attribute to html tag, then javascript logic is applied to any tag that has this attribute. You could make your 2nd version work by adding your own javascript code, similar to how confirm works.
You can see the code that makes :confirm possible here: https://github.com/rails/jquery-ujs/blob/master/src/rails.js
Related
I'm trying to setup a link that deletes a database entry, and everything works as intended, it just doesn't ask for confirmation first. I think I've done everything right, I'm testing this in firefox on ubuntu.
<%= link_to "Delete", #post, :confirm => "Are you sure you want to delete?", :method => :delete %>
any help is appreciated.
Read the documentation carefully:
:data - This option can be used to add custom data attributes.
Data attributes
confirm: 'question?' - This will allow the unobtrusive JavaScript driver to prompt with the question specified (in this case, the
resulting text would be question?. If the user accepts, the link is
processed normally, otherwise no action is taken.
Your link_to should be like this:
<%= link_to "Delete", #post, data: { :confirm => "Question?", :method => :delete } %>
Look at this if you use a version before 4.0.2
Do you have this lines in your application.js file ?
//= require jquery
//= require jquery_ujs
My code here works fine. The line I'm concerned with is :confirm =>
<%= link_to I18n.t('helpers.links.remove_from_your_page'), '#',
:confirm => I18n.t('helpers.links.confirmation'),
:remote_url => reject_review_path(review),
:class => 'btn btn-danger remove_page_button_pos remove-from-your-page',
:id => "remove_from_your_page_#{review.id}" %>
My Internationalization file has:
Helpers:
links:
confirmation: "Are you sure?"
So, when the person clicks the button, before proceeding they are given the confirmation box with 'Are you sure?', with Cancel and OK buttons - works as planned.
The problem is when I want to put spaces between lines on the confirmation box. For example, I want to have:
Are you sure?
If you do this, that might happen.
If you do that, this might happen.
Cancel OK
I thought what I have below would work, but it doesn't:
(note the 'raw', in :confirm etc...)
<%= link_to I18n.t('helpers.links.remove_from_your_page'), '#',
:confirm => raw I18n.t('helpers.links.confirmation'),
:remote_url => reject_review_path(review),
:class => 'btn btn-danger remove_page_button_pos remove-from-your-page',
:id => "remove_from_your_page_#{review.id}" %>
And in my internationalization I have:
Helpers:
links:
confirmation: "Are you sure?<br/>If you do this, that might happen. <br/>If you do that, this might happen."
But I get a syntax error. Any idea how I can get it working? Thanks.
You won't be able to generate HTML markup, but you can certainly insert newlines:
Helpers:
links:
confirmation: |
Are you sure?
If you do this, that might happen.
If you do that, this might happen."
I18n.t('confirmation') #=> "Are you sure?\nIf you do this, that might happen.\nIf you do that, this might happen.\n"
Remember to mind the whitespace and tabs. YAML is very specific about consistency with spacing. The standard is two-space indentation and no tabs.
The confirm-box message appears like it should when the link is clicked.
<%= link_to 'Close Account',
#profile,
:confirm => "Are you sure?",
:method => :delete
%>
When the confirm-box appears, the "OK" button is selected by default; I want the "Cancel" button selected by default, so that if the user accidentally presses enter then the profile record will be safe and will not be deleted.
Is it possible to have the "Cancel" button selected?
The confirm box is generated by Javascript, using an attribute selector in Rails (note data-confirm) on the element. Unfortunately, this box is generated by the browser, so there's no real direct control over it. If you're using the latest version of Rails, I suggest looking into jQuery UI, and using the dialog library. Here, you can make custom confirmations, and maybe even an attribute binding such as data-uiconfirm. Which would look like this:
<%= link_to 'Close Account',
#profile,
:data =>
{:uiconfirm => "Are you sure?"},
:method => :delete
%>
And the attribute binding like so:
$('[data-uiconfirm]').dialog(....)
No. This is a browser implemented thing, and there's no way to change the selected box. If you want to get around this, you would have to make your own popup code where you could change the selection.
I'm diving into Ruby on Rails and I find that when I'm using the scaffold code that RoR generated for my model, it causes a confirmation box to appear that reads "Are you sure?" when I click on the delete link. What causes this to appear and how do I remove it? Thanks, I'm just trying to figure out how this all works.
The scaffold link_to will look something like this:
<%= link_to "Destroy", #code, :confirm => 'Are you sure?', :method => :delete %>
Removing the :confirm => 'Are you sure?', argument to the link helper will remove the javascript confirmation alert.
The link in the template has a :confirm=>"My message here" option. If you remove that option, then it won't have a message.
I use this code to delete record:
<%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method => :delete %>
When I click the "Destroy", it will have an alert box prompt out and ask me to destroy or not. Can I modify the :confirm to an AJAX call? or can I add a :myConfirm in to the link_to tag to implement my own :myConfirm? Thx.
Indeed you can. Create your own helper method (suggested) or extend the default link_to helper (not suggested) in order to include your custom behavior.
If you are wondering whether you can achieve the result simply passing an additional option, the answer is no.
You actually need to code the AJAX call or the feature, then create the helper logic to handle the new behavior.
Best of all, avoid hacking the helper itself and create an unobtrusive JavaScript callback based, for instance, on a specific tag class.
You can easily to this in jQuery
$("ajax-confirm").click(function(){
// here send the AJAX request
// and display the confirm dialog
var confirm = true;
return confirm;
});
<%= link_to "...", "...", :class => "ajax-confirm" %>