rails button_to --- alt text - ruby-on-rails

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.

Related

displaying confirmation box text on a new line in Ruby on Rails

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.

Rails :confirm | How to select “Cancel” by default in confirm box?

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.

Newline in confirmation box

I want to have two lines in my alert box
Right now I have the following:
<%= link_to 'back', 'history.back()', :confirm => 'Are you sure? This is my second line' %>
I have tried adding '\n' but it is not working, it just writes \n between my text. What should I do?
Thank you in advance
I think the single quotes around your confirm text are causing the \n to be interpreted literally. Try this:
<%= link_to 'back', 'history.back()', :confirm => "Are you sure? \n This is my second line" %>
If you want a multi-line confirm/alert statement, you will need to use double quote.
Single quote will not convert your new line feed \n
This would work:
:confirm => "You are about to \n DELETE all tracking information.\nAre you sure?"
Hope this helps ;)
New line in JavaScript alert box You need to stop rails from escaping the new line in the generated HTML form. Can you try <%= link_to 'back', 'history.back()', :confirm => raw 'Are you sure? \n This is my second line' %>

How do I put a delete link within an update form?

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.

How do I disable the popup box that says "Are you sure?" when I delete a row from my DB in RoR?

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.

Resources