How to make a DELETE link to a rails resource? - ruby-on-rails

If I want to make a DELETE link in rails, I write the following code (in this example case to delete a user session in Devise):
<%= link_to('Logout', destroy_user_session_path, :method => :delete) %>
That will turn into the following HTML:
<a rel="nofollow" data-method="delete" href="/users/sign_out">Logout</a>
Works great, except I can't simply put that HTML into a static HTML page. It will simply do a GET request. I assume Rails includes some standard Javascript to turn the above link to one that actually does a DELETE request. So what's the proper way to have a link on a static HTML page to a Rails resource that does a DELETE action? Should I find and grab that Javascript Rails includes in all webpages that does this? Is there a better way?

You can't send a DELETE request with an anchor link, unfortunately - a traditional anchor link will only send a GET request. Actually, you can't really send a true DELETE request at all. If you want to make a delete link, without javascript, the solution is fairly easy. Check out 2.4 How do forms with PUT or DELETE methods work? in the official documentation. Basically, you can simply create a form that submits to the url of your resource, with a method of delete. It's pretty simple, and you don't need to rely on javascript to get the job done. Hope this helps, good luck.

Rails uses JavaScript to handle delete links.
It creates a invisible form, and submits it when you click a link that has a data-method attribute of "delete"
Have a look at handleMethod in https://github.com/rails/jquery-ujs/blob/master/src/rails.js
Maybe your delete links in your static pages will just work when you include rails.js (if you are using jQuery).
If not, you can build a form as it is done in handleMethod on your own.

Since some of my colleagues are working on a native app where the user may not have the JavaScript enabled on their browsers, in such cases the delete link won't work and we would get the show page instead of delete....So if you are unsure whether the user may or or may not have the JS enabled on its browser, you can always generate a route with GET or POST method to the delete action of your controller...This approach would work for the static HTML pages also..

Related

No route matches /signout when i disable javascript

To signing out a user i have the link :
link_to "Sign out", signout_path, method: "delete"
this link work fine and i have already a route /signout in my routes file, but when i disable javascript and click this link it says : No route matches [GET] "/signout", what's the problem here ?
Without scripting, browsers can only send a non-GET request by submitting a form -- clicking a link is always a GET. When you specify a different method for link_to, it adds a JavaScript hook to the link that dynamically creates and submits a form with the appropriate method. If scripting is disabled, the browser just submits a GET request as it normally would. This is explained in the docs here (look under Options).
If your app needs to support users with scripting disabled, you can use button_to instead of link_to, and style the button to look like a link.

Embedding URL in Ruby Rails?

I'm having a bit of an issue linking to a web app in my Rails application. Basically, I have a layout for the application, made of a header, sidebar, and main_content, and I want to be able to open the web app in the main window, sort of like a frame would do. How do I go about this? I tried using a simple link_to command, but that opened a new window, even when I included the :target parameter. I tried to set up a controller for it, and use that, but I couldn't find a way to link it to the app. Note that while I do own the app, I can only access it through a URL: I don't store it locally.
Sorry if this is an amateurish question, I'm still learning rails, and I'm very much in the beginner stage.
Assuming that the link you're using is to a full webpage rather than a fragment that's designed to be used inside another page, your best bet is an iframe embed.
All link_to does is render a regular hyperlink, and that will always load a new page (whether it's in a new window, tab, or replaces the current page).

Grails- Add link href to button

I'm building a simple Grails app for a web development class. For the most part, the app is finished, but I'm having one sticking issue.
On the index page, I have a series of buttons that correspond to the List, Create, and other templates that are built in Grails via scaffolding. How can I dynamically pass on the correct path to the controller action?
In order to do this, I need to get the current page URL and add the proper location. Is that possible to do in Grails or should I stick with jquery or some other ajax solution?
What are you trying to achieve here,
If you want to generate link to controller actions that you can use for button href, you can do it like this,
<button href="${createLink(controller:'foo', action:'bar')}"/>
See the createLink tag
If you want to know controller and action name, ${controllerName} and ${actionName} can be used.
Can you use ${controllerName} to get the name of the current controller?
An alternative might be ${params.controller}, but again, not 100% sure it works in gsps

Adding html form and input tags into Symfony static pages

I inherited the management of a Symfony site and need to add some HTMl form tags to one of the "static" pages via the CMS. The scenario I have is:
/index.php/splash/welcome pulls up the welcome screen.
We want to be able to add a subscription button on that page.
The HTML has been supplied for us by the company that handles the subscriptions.
The form post method has an action that references a script on a remote site (no lectures on the security implications please ;-).
When I add the <form... and <input... tags via the CMS admin panel, the tags get removed automatically by Symfony.
Is there a way to tell Symfony to allow these tags?
Thanks in advance,
Marty.
This is goign to depend completely on how the developer set up the CMS. Youre using a rich text editor in source mode i would take a look at that editor's config file and documentation because its probably the one responsible for stripping the tags.
If its just a plain text area i would check the submit action for the edit form and take a look at the code... he may be using something to strip them in there.
If youre using one of the Symfony CMS plugins (Diem, Apostrophe, Sympal) i might be able to help further if i know which one youre using. If its something custom we would need to see the code. This isnt really indiciative of the Symfony core, but rather the CMS youre using.

Can i redirect_to in a new window

I have a list of links that users can click. When a click is made, I want to increment a counter (to track how many users clicked that link) and then open the link in a new window.
Right now, in the 'show' method method, I can do all that except the "new window" thing. Is there a way to achieve this in pure rails or do I have to do put some additional javascript to launch an ajax request (for the incrementation) and then open the link in a new window?
thanks,
Pierre
I would think the easiest solution would be to just have your link contain
target="_blank"
The window would open, go to your counter page, and get redirected to the correct page
You need to use AJAX, either to return javascript from the server or just a success code, and then open in a new window from javascript.
HTTP provides no way to open a new window.
A note! Be SURE you're a tags still have hrefs pointing at the URLs for your SEO!
Good luck!
A simple javascript may do:
window.open('filename.php?var=' + varvalue, 'resizable=1, scrollbars=1, left=80,top=60, width=650, min-height=400');
If you are using any jquery libraries, you can look into modals.
I do know about how to do this in Rails.

Resources