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.
Related
UPDATE - I just checked and NEITHER confirm is working.
I need to have a confirm on a link_to. I've tried a couple of variations on the data/confirm attribute but it is bypassing the confirm dialog.
My link is
<%= link_to 'new bs line',
:controller => :bedsheet_lines,
:action => :create_new_bedsheet_line,
:data => {:confirm => 'Are you sure that you want to export the Bedsheets?'} %> <br>
That does not work, but a regular link_to does: and neither does this
<%= link_to "Export Data as a CSV file ",
slitter_bedsheet_export_path(format: "csv"),
:data => {:confirm => 'Are you sure that you want to export the Bedsheets?'} %> <br>
If I do an inspect, the link that won't show the confirm looks like
<a href="/bedsheet_lines/new_bedsheet_line?
data%5Bconfirm%5D=Are+you+sure+that+you+want+to+export+the+Bedsheets%3F">
new bs line</a> <br>
Whereas the link that does show a confirm looks like Nor dies this work.
<a data-confirm="Are you sure that you want to export the Bedsheets?"
href="/csv_export.csv">Export Data as a CSV file </a> <br>
My suspicion is that the problem is that confirm will not work when your link_to specifies and action and controller. If that is true, I should be able to find a way to work around that. Now I'm not sure where my problem lies.
Not sure why the hash syntax matters but I use
<%= link_to 'text', path(param), data: { confirm: 'confirmation' } %>
Edit:
Specifying controller and action it would be:
<%= link_to 'text', { controller: 'bedsheet_lines', action: 'create_new_bedsheet_line' }, data: { confirm: 'confirmation' } %>
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.
I've been searching for a while for a solution, but haven't been able to find out what is wrong with my code.
I'm trying to delete an item using an ajax call on an event page, but it doesn't seem to work.
<%= link_to 'X', { :controller => 'items', :action => 'destroy', :id => item.id }, :method => :delete, :confirm => 'Are you sure you want to delete this item?', :remote => true %>
<a rel="nofollow" data-method="delete" data-confirm="Are you sure you want to delete this item?" href="/items/83">X</a>
I believe everything looks right, however, the data attributes don't seem to work. No confirmation occurs when I click this and instead it calls the 'show' action for the controller, so the data-method is not working either.
The weirdest part is that I something else on the page that generates the confirmation messages.
<%= link_to image_tag("trashcan.png"), #event, :confirm => 'Are you sure you want to delete this event?', :method => :delete %>
<a rel="nofollow" data-method="delete" data-confirm="Are you sure you want to delete this event?" href="/events/3">
I've tried changing the 'X' in the first one to an image (in case an image_tag did the trick) - no luck. However, when I changed the link_to to a button_to it did delete, but not confirmation which I assume button does not support. So I'm not sure if I maybe has some javascript error somewhere else, which I assume to not be the case since it works in the other case or maybe I'm missing something somewhere.
Edit:
I forgot to mention I also have a comment for each item and that works as well...
<%= link_to 'X', [comment.item, comment], :confirm => 'Are you sure?', :method => :delete, :remote => true %>
<a rel="nofollow" data-remote="true" data-method="delete" data-confirm="Are you sure?" href="/items/83/comments/32">X</a>
Fixed it, the problem was I had a div behind these links where I bound an click event to and in which I also called event.stopPropagation() thus prevented the data calls to be made.
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.