html text in twitter bootstrap modal data attributes parameter - ruby-on-rails

This comes from Twitter Bootstrap modal rails delete button not working
How can I pass html code to show in Twitter Bootstrap modal? Here is the link
<%= link_to t('delete'), post, method: :delete, confirm: t('delete_this_question'), 'data-my-message' => raw(post.text), class: 'label' %>
post.text is HTML code. Now it shows link in bad format.
Thanks

I think the problem here is probably your use of "raw". If you use raw html inside your link_to helper it will probably mess up the link's quoting. For example you might get something like this:
world" ...>
And that would mess up your link tag. I'm not sure how the rest of your view fits together but I think you should just be able to drop the 'raw' method:
<%= link_to t('delete'), post, method: :delete, confirm: t('delete_this_question'), 'data-my-message' => post.text, class: 'label' %>

Related

send post params with a link_to in rails

I want to call an api endpoint on my rails app.
I cannot find the way to pass post params from link_to nor from form_tag
link_to '/v1/my_endpoint/approve', data: { confirm: "Are you sure?" }, remote: true, method: :post, id: 'approve-id' do 'link_name' end
I want together with the above link to pass some post params.
link_to isn't going to do what you need. You could pass query params that way, but not form params.
You need to use a form, or write some javascript to submit the request for you.
Rails has a handy button_to helper which will create a small form, presented in your UI as a single button. You can add params to that quite easily:
<%= button_to "button label", "/v1/my_endpoint/approve", remote: true, params: { id: "approve-id", something: "else" } %>
If you wanted, you could use CSS to style that button as a link.
You can read about the button_to method in the docs here:
https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to

:confirm in rails not working

I just started coding in ruby on rails and I've been following a guide which is using a more outdated version of rails than I am using. I am using 3.2.12
This is my code:
<%= button_to 'Destroy', product, :method => "delete", :confirm => 'Are you sure?' %>
From what I understand, these are symbols that are passed to rails, which is then converted to either an html or javascript action that then pops up the message box and deletes the object if applicable. The above code destroys the object, but it does not pop up the confirm box. Why is this? Also, I had the above as the following at first:
<%= link_to 'Destroy', product, :method => "delete", :confirm => 'Are you sure?' %>
The confirm box is not popping up under any circumstance, using link_to or button_to. Below is the html rendered when inspected using Chrome's inspector. jquery and jquery-ujs are loaded into the as well, so I'm not sure where to go from here.
<input name="_method" type="hidden" value="delete">
<input data-confirm="Are you sureeee?" type="submit" value="Destroy">
<input name="authenticity_token" type="hidden" value="Q2xicqELHYHtrwarbtPBe5PT2bZgWV5C+JdcReJI8ig=">
Thanks!
I had to add my confirm attribute inside the data attribute to get it to work. I am using rails 4 with bootstrap. I hope this helps someone else who has that issue.
link_to 'Delete', #rule, method: :delete, data: { confirm: 'Are you sure you want to delete this alert?' }
This relies on jQuery, ensure you have the following:
in your Gemfile
group :assets do
gem 'jquery-rails'
end
in your assets/javascripts/application.js file, before the line //= require_tree .
//= require jquery
//= require jquery_ujs
The difference between link_to and button_to is the HTTP verb. link_to issues GET requests and button_to issues POST requests. With the RESTful routing, the delete action is a POST request to controller/id. If you issue a GET to controller/id, it is dispatched to the show action.
AIUI, link_to with anything other than the GET verb is a Bad Idea. One, right-clicks don't preserve the verb. Two, you don't want bots crawling the page to follow the link and thereby trigger the delete action even though you probably need to be logged in to actually modify the database.
Feel pretty dumb, but adblock was blocking the message box. Sorry about that. All is well now, I just disabled adblock.
If you want to delete something with confirmation box in rails 7, you may try this one:
With button_to (more prefered IMHO):
<%= button_to 'Destroy', product, method: :delete,
form: {data: {turbo_confirm: 'Are you sure?'}} %>
This will render an HTML form tag which sends a POST request on submit (after confirmation) but with a hidden _method attribute with 'delete' value. So that rails will treat this request as if it has a DELETE method.
It will be routed to products#destroy (or whatever your routes are saying).
With link_to:
<%= link_to 'Destroy', product,
data: {turbo_method: :delete, turbo_confirm: 'Sure?'} %>
This will render a simple a tag with data-turbo-method and data-turbo-confirm attributes. Clicking this link will trigger a confirmation box and if "OK" is chosed, a real DELETE request will be sent.
If you want to end destroy action in your controller with a redirect_to, some browsers will redirect to a new location with DELETE method (causing errors), so make sure to add status: :see_other parameter, like the guides suggests.
I have a pop-up blocker running in Chrome. I just whitelisted http://localhost:3000 and it worked for me.

Submit form using link_to in rails

I'm trying to submit a form using link_to as follows:
<%= form_for(#post, :url=> '/post/action', :method=> 'post', :html => {:id=>'form_id'} ) do |f| %>
....
<%= link_to 'submit', "/post/action", :onclick=>"document.getElementById('form_id').submit()" %>
....
but it is not posting the form, it is simply redirecting my form to the specified url. Does anyone know how to do this?
You can use:
<%= link_to 'submit', "#", :onclick => "$('#form_id').submit()" %>
if you are using JQuery and a later version of rails.
Above will work but if you have a really long page it will navigate to top of the page because of "#" so if you want to avoid that you can do:
<%= link_to 'submit', "", :onclick => "$('#form_id').submit()" %>
I think both things happen. The browser starts to submit the form, but it also follows the link's href. You can fix it by linking to # instead of /post/action...
...however, I don't recommend doing it. There are a few better approaches:
First, you can use a button instead of a link. You'll have to style it to make it look like a link, but that should not be a problem. It will be better, because it won't break the Principle of Least Surprise (people who read the code expect forms to be submitted with buttons) and you won't need the JavaScript.
If you insist on using a link, you should at least move the JavaScript code from the view to a JavaScript file. Then have this behavior added unobtrusively (although, you won't have a good fallback with the link). Assuming you're using jQuery, it should be as simple as:
$(document).on('click', '[data-submit-form]', function(e) {
e.preventDefault();
$(this).closest('form').submit()
}

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.

How to modify the :confirm or add a new method to link_to UrlHelper?

I use this code to delete record:
<%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method => :delete %>
When I click the "Destroy", it will have an alert box prompt out and ask me to destroy or not. Can I modify the :confirm to an AJAX call? or can I add a :myConfirm in to the link_to tag to implement my own :myConfirm? Thx.
Indeed you can. Create your own helper method (suggested) or extend the default link_to helper (not suggested) in order to include your custom behavior.
If you are wondering whether you can achieve the result simply passing an additional option, the answer is no.
You actually need to code the AJAX call or the feature, then create the helper logic to handle the new behavior.
Best of all, avoid hacking the helper itself and create an unobtrusive JavaScript callback based, for instance, on a specific tag class.
You can easily to this in jQuery
$("ajax-confirm").click(function(){
// here send the AJAX request
// and display the confirm dialog
var confirm = true;
return confirm;
});
<%= link_to "...", "...", :class => "ajax-confirm" %>

Resources