Rails 3 data errors with link_to - ruby-on-rails

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.

Related

Rails 7 - Devise do not delete user account

My View:
<%= link_to 'delete account', user_registration_path, :method => :delete, :class=>
'delete_account_button', data: {:confirm =>'sure?', disable_with: "deleting..."} %>
and I'm redirected to localhost:3000/users ... nothing more. No console errors, logs just show that I was redirected ...
FIRST WORKING EXAMPLE:
Changed link_to to button_to and it work. Why?
From the rails guides, you need a data turbo method to make it work
<%= link_to "Destroy", article_path(#article), data: {
turbo_method: :delete,
turbo_confirm: "Are you sure?"
} %>
So update from data key with something similar
data: { 'turbo-method': :delete, 'turbo-confirm': 'Are you sure?' }
turbo documentation
button_to
Is used to submit a form and send either a POST or DELETE request
link_to
Is used redirect_to a page through GET requests
Turbo is not necessary here to make the button work. However if you wanted to reload elements on the dom you would need turbo.
Check this tutorial out if you want to learn turbo and Hotwire for Rails 7.
https://www.hotrails.dev/turbo-rails
It helped me out a lot

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.

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.

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

Resources