Newline in confirmation box - ruby-on-rails

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' %>

Related

link_to confirm not working with controller/action

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' } %>

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 button_to --- alt text

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.

Rails - Creating a Delete Link

I'm using the following for a delete link:
<%= link_to "Delete", photo_path(#photo.id), :confirm => "Are you sure?", :method=>:delete %>
Problem is, if I click CANCEL - it still deletes!!! wooops... Why is that?
First, you don't have to write photo_path(#photo.id), just pass #photo, that's enough.
<%= link_to "Delete", #photo, :confirm => "Are you sure?", :method => :delete %>
If the link doesn't work, that means that your page doesn't correctly load the JavaScript adaptator for your JavaScript framework. For example, if you use use jQuery, check this :
http://github.com/rails/jquery-ujs

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