I'm using RestfulRoutes written by Steve Hodgkiss. So far so good but I do have a question. I have a Session Controller that I want to use to destroy a users session when they click the logout button. How can I do this with and action link?
So far I have the following.
<%= Html.ActionLink("Logout", "Destroy", "Session") %>
I need to hit the following route.
Session/{id} and have a DELETE constraint.
I hope this makes sense.
Mike
An anchor tag in HTML can only ever issue a GET, it cannot issue any other verbs (unless you intervene with JavaScript).
Related
Rails provides data-disabled-with for button tags on form submits. Super awesome. However, link tags don't get this desired behavior. They're great to prevent users from clicking a button too many times and producing an unwarranted effect.
Is there a way I can do something like:
Purchase me
Rails provides this functionality via the :disable_with parameter of the link_to helper. See docs.
For example:
link_to("Create", create_post_path(#post), remote: true, disable_with: "Creating...")
Recognizing of course that creating a resource via a GET request isn't idiomatic Rails/REST... but this hopefully illustrates how it could be used.
From what I understand, link_to is used for get methods, and button_to is used for post methods.
On the other hand, I was told that with HTML5 semantics, <button> is used for any type of clickable...well, button. In the case I have a clickable button that sends a user to a form to fill out, should I create a button_to or a link_to?
It's simpler that you think.
That methods are Rails helpers and don't have anything to do with haml.
Yes, one method is for get and another for post methods. If you need to post any data to controller, use button_to (for example when deleting a record). Otherwise, link_to is enough.
Moreover, you can make link_to posting data using :method parameter:
= link_to "Something", some_path, :method => :post
Answering your question, use link_to.
The main principle difference between the #link_to, and #button_to is that the #link_to just creates a link tag A, and makes simple AJAX request without an additional data, while #button_to creates a FORM with a custom data, so the form can be used to make extended AJAX request to a webserver. The form data includes embedded CSRF-token, which is used to authentication the request. In case of #link_to CSRF-token must be serualized and send in on_click event.
You should use links to point the user to a resource, like an article.
But you have to tend to use buttons to point to an action(like "Create"/"Send" on your edit page). If this doesn't agree with your interface -- style them like as a link.
Here's why: you cannot point your user to any non-GET action via link_to if he lacks the javascript support. So, buttons are the only options to make your send/destroy action to be triggered in this case.
Feel free to use both approaches if your link points to a page that eventually leads to a modification of a resource (link/button to an edit/create page that shows a form), like in your case.
If you want to simply send a user to somewhere, it is get request. So you should use link_to in this case. By the way, you can use the link_to for post requests and other requests (like button_to too) if you will specify :method. For example: =link_to "some path", some_path, :method => :get
I want to pass the id from one action to the next action, but I do not want it seen in the URL. Is there a way to hide it?
Using Symfony, I have created a sign-up done page whose URL should be /signup/done
When the form is processed, I want to redirect to the signupSuccess action and pass the recently created ID for future use. So I use...
$this->redirect('#signup_done?id=' . $sign_up->getId());
The routing is as follows:
signup_done:
url: /signup/done
param: { module: SignUp, action: signupDone }
I have avoided the :id at the end because I don't want it in the URL.
But the resulting URL is /signup/done?id=1
Just as an experiment, I tried putting this on a template.
<?php echo link_to('Sign-up again', '#signup_done?id=1', 'post=true') ?>
Even when using post, the query parameter appears in the URL.
The need is: I want to pass the id from one action to the next action, but I do not want it seen in the URL. Is there a way to hide it?
I set the id as a parameter in the request using $request->setParameter('id', $id) and it was available in the next action.
This kept the URL clean.
If you want to post, you need a form. Back in symfony 1.2 there were helpers that you could call and made you just that - but they were removed for the promotion of promoting good code.
Depending on how you want the 'Sign up again' text to look, you can either create a simple form and a submit button, or create a link, attach a click listener, and create a form there via JS, finally post it.
Any parameter that you pass to the route in url_for, link_to and such end up in the get parameters.
I have a list view in my RoR web app. The view is a result of a scaffold operation.
I want to add a button that will refresh the page. I prefer not to use javascript for this one, is there another way?
Creating a button via button_to or form_tag ends up adding a new blank row in the database when it is clicked.
Thanks!
Shay.
I am going to assume that you are using standard RESTful routes and as a result your form/button_to calls are POSTing to you controller and therefore executing your create action. If that is the case then either of the options you tried should work, but just make sure you are setting the method to "get" so the request is routed to the index action correctly
<%= button_to "Refresh", my_path, :method => :get %>
1) Use a link and dress it up as a button w/ CSS
or
2) Use a form whose method is "get", points to the current page (without any params) as the action and has only one element, a submit button?
What is the most straightforward mechanism by which to "link_to_remote" and popout a window with the data? I need "link_to_remote" because I need to pass certain data along with the url without redoing my routes. If this would be better served by a "link_to" without the need to redo my routes, I'm all for it. I simply can't get it to work, atm.
Thoughts?
Perhaps you're looking for something like:
<%= link_to_function "Show Article in Popout window",
"window.open(#{article_path(article).to_json}, 'show_article')" %>
link_to_remote is used to do XHR requests, so you cannot use it to open a popup window with the response.
You don't need to redo your route anyway, because I assume that if you want to use link_to* to get some data, then you have your controller/action pair defined and accessible already.
Also, usually, Rails applications have the catch all route enabled to match :controller/:action. If that's your case, then you can use link_to that controller/action to get the data.
What window do u mean ? modal window ? if u want to show some data through modal window, then i suggest u to use http://prototype-window.xilinus.com/index.html, it easier to use, just write some js function and create link_to_function in rails helper/view to call the js function.