Rails Minitest system test to accept dialog box - ruby-on-rails

Rails minitest system test with a modal/confirmation dialog box.
How do I accept/reject it?
link_to "Delete", user_path(#user), method: :delete, data: { confirm: "Are you sure" }

While there is no Minitest specific solution System tests use Capybara which supports modal dialog boxes with the following syntax:
accept_confirm do
# test what happens on confirmation
end
There is also an option to verify the displayed prompt:
accept_confirm 'Are you sure?' do
# test what happens on confirmation
end
There are plenty of other ways of working with modal dialog boxes:
dismiss_confirm do
# test what happens on dismissing
end
See documentation for accept_alert, dismiss_confirm, accept_prompt
Documentation
Introduced Capybara 2.4.1

Related

Change the confirm modal in Rails 6 link_to

Is there a way to change the { confirm: 'Are you sure you want to report this challenge?' } alert thing, like a modal in the middle of the screen with an input where I can write something?
<%= link_to(report_post_path(#post), method: :post, data: { confirm: 'Are you sure you want to report this post?' }) do %>
Or I have remove the data: { confirm: 'Are you sure you want to report this post?' } and make the modal myself?
you can try gem data-confirm-modal, it is have good documentation.
Also you can check any solution from gists, I think it will be good gist.
Third option to search other solutions on google with request rails confirmation modal. But for me the gem the best solution

RSpec/Capybara can't find link to image

I want to use RSpec and Capybara to simulate clicking on an image, but I cannot do it.
My error is this:
Failure/Error: click_on "icon_red_heart.png"
Capybara::ElementNotFound:
Unable to find visible css "#image-button"
spec/features/posts_spec.rb
scenario "push an image" do
visit posts_path
expect(page).to have_current_path(posts_path)
find('#image-button').click
end
likes/_likes.html.erb
<%= button_to post_like_path(post, like), id: "image-button",method: :delete, remote: true do %>
<%= image_tag("icon_red_heart.png")%>
I don't know how to how to specify the image.
Please teach me.
As #jonrsharpe indicated, you are not referencing the button properly. The most resilient way to reference an element is to give it an id and reference it by that id.
Also, the button created by button_to may have no content, in which case you'll need to tell Capybara that the button is not visible.
Change the button_to line to this:
<%= button_to post_like_path(post, like), id: "image-button", remote: true do %>
Then change your test to read as follows:
scenario "push an image" do
visit posts_path # Use the name of your path here
find('#image-button', visible: false).click
end
Incidentally, using method: :delete in your button_to link doesn't do what you would expect. The method is automatically set to POST, which is presumably what you want.

Issue with delete action

I have a problem with my delete action and link. Here is my link :
<%= link_to 'Supprimer la thématique', #theme, method: :delete, data: { confirm:'Êtes-vous sûr de vouloir supprimer la thématique' }, :class => "btn btn-default" %>
And I don't know why, but this link redirect to the show action of my model 'theme'.
I can resolve this by using button_to but with a button_to I'm not able to set a data confirm :
<%= button_to "Supprimer la thématique", #theme, :method=>:delete, data: { confirm: 'Êtes-vous sûr de vouloir supprimer la thématique ?'}, :class=> 'btn btn-default' %>
Do you have any ideas why the link to doesn't redirect to the delete action or why I can't have a data confirm for my button_to ?
Clicking a link always sends a GET request, unless there's Javascript in place that does something different, such as sending an Ajax request or submitting a hidden form. When using regular resourceful routes, the show action and destroy action share a route, and determine what to do based on the method (as you probably know).
The button_to method should support a confirmation dialog; if it doesn't work, then there's probably some Javascript missing.
Both your problems point to a missing unobtrusive Javascript driver. If using jquery-rails (the standard option), make sure that it's listed in your Gemfile:
gem 'jquery-rails'
and that you're loading it in your application.js:
//= require jquery
//= require jquery_ujs
Also be sure that you're calling the javascript_include_tag method in your application layout. Once you've got jquery-rails loaded properly, you should be able to get the results you're looking for with either method you've attempted.
For more information on loading jquery-rails, see the Github repo.

Confirm boxes not working for delete links only

I am currently banging my head against the desk trying to figure out why this is not working. I am trying to get the ujs confirmation box to show up when a delete link is clicked. Currently, the item is deleted with no confirmation box. Here is my delete link:
<%= link_to "void", project, method: :delete, data: {comfirm: "Are you sure you want to delete this project?"} %>
Here is where it gets strange. The following link (not a delete link) works as expected:
<%= link_to "About", about_path, data: {confirm: "test test"} %>
I did some digging in the gem itself and was able to discover that, with the delete link, the data-message attribute is not being parsed correctly in the following code. Specifically, the
if (!message) { return true; }
is returning true, where message is defined as follows:
message = element.data('confirm')
Note: element is the entire link itself. Can anyone help me find out why this is happening? I am using Rails 3.2 if it helps.
I have done this, just make it confirm instead of comfirm, and it works fine.
<%= link_to "void", project,method: :delete, data: {confirm: "Are you sure you want to delete this project?"} %>

How to make confirm message i18n compatible

I'm relatively new to Rails, and have been programming a few months.
I'm trying to use the t() method for internationalization, but it doesn't seem to work when I ask for confirmation in a link_to.
For example, when I write
<%= link_to t( ".delete_student_info"),
#student,
method: :delete,
confirm: "child_deletion_confirmation"
%>
...I predictably get a link_to that works and asks the confirmation question
However, when I write
<%= link_to t( ".delete_student_info"),
#student,
method: :delete,
confirm: t( ".child_deletion_confirmation")
%>
...I get the following output
Child Deletion Confirmation" data-method="delete" href="/en/student_profiles/41" rel="nofollow">Delete Student Info
Is there something conceptual that I am missing? I've looked in the Rails Guides Rails i18n API, but it doesn't address this issue. I'm thinking that maybe the confirm: is something different, but I don't know how to look it up. Any ideas?
I tried this out on my Rails 4 console and it worked fine:
helper.link_to "Visit Other Site", "http://www.rubyonrails.org/", data: { confirm: I18n.t("date.formats.default") }
# => "<a data-confirm=\"%Y-%m-%d\" href=\"http://www.rubyonrails.org/\">Visit Other Site</a>"`.
Now, note the behavior when using nil for the :confirm is like what you're seeing:
helper.link_to "Visit Other Site", "http://www.rubyonrails.org/", data: { confirm: nil }
# => "Visit Other Site"
So this makes me think that somehow your translation is evaluating to nil. However, I can't seem to figure out how to duplicate that issue...
I'll expand this answer to try to help more if you can show what the translations file looks like?

Resources