Change the confirm modal in Rails 6 link_to - ruby-on-rails

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

Related

Rails Minitest system test to accept dialog box

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

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?"} %>

Hide link in rails based on condition

I have an Opportunity and a User model. A User can log in as an admin (I have admin as a boolean attribute). I want users to be able to delete opportunities if and only if they are an admin and I was wondering if anyone had any idea how to do this? So far I have the following delete link for my opportunity:
views/opportunities_opportunity
<%= link_to_if(#user.admin?, "Delete", opportunity, method: :delete, data: {confirm: 'Are you sure?'}) %>
However, I keep getting the error "undefined method `admin?' for nil:NilClass"
Please help. Thanks!!
fixed... I use #current_user.admin? instead and it worked.

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?

Abstract the delete link for a polymorphic model used in a nested controller

Question
In a polymorphic model, thats used in nested controllers, how can I abstract my delete link's path so I'm not hardcoding upload_permitted_user_path(#permissible, permitted_user)?
Details
I have a polymorphic model called permitted users. Basically theres a bunch of objects in my application where we need to control who can see it. So a post, photo, etc can have permitted users.
I want to be able to delete permitted users on the post#edit, photo#edit, etc pages.
I have this line:
# Used in "posts#edit"
<%= link_to 'Delete',
post_permitted_user_path(#permissible, permitted_user), # This should not be hardcoded.
method: :delete,
data: { confirm: 'Are you sure?' } %>
# Used in "photos#edit"
<%= link_to 'Delete',
photo_permitted_user_path(#permissible, permitted_user), # This should not be hardcoded.
method: :delete,
data: { confirm: 'Are you sure?' } %>
How can I abstract the path so I'm not hardcoding <MY_TOP_LEVEL_CLASS>_permitted_user_path(#permissible, permitted_user)?
Found the answer (feel free to repost and I'll accept :P)
Creating polymorphic links is easy using "polymorphic routes".
You can easily generate the proper link using polymorphic_url([#top_resource, #next_level_resource]) in any view.
For example:
polymorphic_url([:admin, #article, #comment]) becomes admin_article_comment_url(#article, #comment).
Another example without the leading :admin:
polymorphic_url([#article, #comment]) becomes article_comment_url(#article, #comment).

Resources