How to create an alert pop up using rails ? Currently I created an alert view file and the code is described following.I need to implement these error Pop up when a delete button pressed and then it should shows alert view. Can you please help me ?
I need a custom pop up alert. can you give an example for custom pop up and how it implemented ?
To add a confirmation on delete button being clicked, use confirm option
<%= link_to "Delete", {:controller => "home", :action => "delete_user"}, :confirm => "Remove this user ?" %>
You can simply define the alert (conformation pop-up) in the view itself.
Example:
<%= link_to 'Destroy',<path>, :method => :delete,
:confirm => 'Are you sure?'%>
On pressing the Destroy button a message would pop up asking Are you sure?
Related
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
I need to add an alt text to a button_to tag.
Here is my button code:
<%= button_to 'Reject' , reject_paper_path(paper), :confirm => 'Are you sure you want to reject this paper?' %>
I would like a text to appear when user does mouseover on the button.
Any suggestion ?
Thank you
Got it:
<%= button_to 'Reject' , reject_paper_path(paper), :title=> "title text", :confirm => 'Are you sure you want to reject this paper?' %>
You simply need to add a title option.
Hope it might of help to someone else.
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 have a remote form for updating a 'Point' model. It's a pretty ordinary form with a submit button.
However, I'd like to also include a 'remove' button beside the 'update' button.
Unfortunately, there is a problem with this. When I click on the 'update' button it ends up deleting the entry - the delete link seems to hijack the update form.
EDIT: I think I know why the update button is deleting. When I add the delete link to the form it adds this input:
<input name="_method" type="hidden" value="delete">
Regardless of whichever button I press this "_method" param is being picked up!
Now, I know that I can just position the remove button outwith the form element but in this case I'm not allowed to.
I guess the remove button could just be another update submit button, but with an extra :remove_this parameter.
However, something about that doesn't feel right. Any ideas?
Another curious workaround that does not require javascript, is to put the delete-form outside the update-form but leave a <label for="theSubmitButtonInTheDeleteForm"> inside the update form.
The update-form will continue to work as expected, but clicking the label will submit the delete-form.
Then just style the label as a button.
I would recommend using a link rather than a button and style it like a button:
link_to("Remove", resource_url, method: :delete, class: "delete_button")
In Rails 3 the recommended way to submit forms using Ajax is to use form_for in combination with UJS rather than remote_form_for. Please see this railscasts episode for more info.
Then when the document is ready/loaded, you add a click listener to each of the buttons, and act accordingly:
$('#id_of_delete_button').click(function() {
// serialize the form and submit it to the delete action
});
$('#id_of_update_button').click(function() {
// serialize the form and submit it to the update action
});
The given code snippet is in jQuery, but you can use Prototype in a similar way as well.
Basically, the only difference between the 'update form' and a 'delete form' is the presence of <input name="_method" type="hidden" value="delete">
So here's the workaround I came up with:
<%= form.submit 'Update', :id => "point_#{point.id}_submit", :style => "" %>
<%= form.submit 'Remove', :confirm => 'Are you sure?', :id => "point_#{point.id}_remove" %>
remove_button.observe('click', function(event)
{
form_element.insert('<input name="_method" type="hidden" value="delete">');
}
= f.button :submit, class: "btn-primary"
= link_to "Delete", f.object, class: 'btn btn-default', data: {method: 'DELETE'}
Delete link is styled with Bootstrap to look like a button. jquery-ujs hijacks clicks on links with data-method attribute and sends them with custom HTTP method.
You could do this:
<%= button_to 'Delete', point_path(point), :method => 'delete', :confirm => 'Are you sure?' %>
you would make a link_to 'del', Variable, :confirm => 'Are you sure?', :method => :delete
The :method => :delete will call the destroy part of your controller.
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" %>