I am confused about the main difference(s) among link_to
, redirect_to
and render
in Rails. anyone can please explain.
相关问题
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- Eager-loading association count with Arel (Rails 3
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
相关文章
- Ruby using wrong version of openssl
- Right way to deploy Rails + Puma + Postgres app to
- AWS S3 in rails - how to set the s3_signature_vers
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
- “No explicit conversion of Symbol into String” for
- form_for wrong number of arguments in rails 4
I actually just wrote a blog post about this. The most important bits are copied below (with modifications).
Controller Methods:
render
vs.redirect_to
render
andredirect_to
are the two ways that controller actions end (generally speaking). To understand how they work, let's review what controllers do in a Rails app:(http://localhost:3000/books/index.html)
(
GET /books/index.html
)(
books GET /books/index(.:format) books#index
)This last step occurs explicitly when you call
render
orredirect_to
, or implicitly if you leave it out.That is,
is the same as
render :index
says, ‘combine the data I've prepared (@books = Book.all
) with thebooks/index.html.erb
view template to generate a complete HTML document, then send that back to the client.’redirect_to @book
says, ‘tell the client to start the whole process over again, issuing a newGET
request tourl_for(@book)
.If you omit both, the action will render a template with the same name as the action itself. In other words, you only need to call
render
explicitly when the view template you want doesn’t match the action you’re rendering it from.Note that not every controller action has a corresponding view template. Generally,
#create
,#update
, and#destroy
(which are all routed to non-GET
HTTP requests) attempt to make some change to the database and then eitherredirect_to
some resource (if it succeeded) or re-render
the form that preceded it, along with any errors (if it failed).As the official guides explain (emphasis mine),
View Methods:
render
vs.link_to
render
is also used within view templates themselves. Rather than generating a complete HTML document, it's used to insert a partial view template into a larger one. Here's the upshot:_nav.html.erb
).render 'nav'
if you want to include the_nav.html.erb
partial from a view located in the same folder.render 'shared/nav'
if you want to include the partial atapp/views/shared/_nav.html.erb
from any view in your project.link_to
is just a convenience method for inserting anchor tags (a href
tags) into your view templates. This is useful because a lot of the URLs you'll want to link to are other pages within your application, and those URLs can be referenced using objects or "helper methods", like so:link_to will output a standard html anchor=a link (link_to documentation)
redirect_to is commonly used for page responses such as update and delete. It will take the parameters you give it and will direct your page appropriately. (redirect_to documentation)
render is used for loading partials or loading specific .erb files into others. (render documentation)
There are a bunch of examples on this rails guide which should explain render and redirect_to. link_to is pretty different from rendering and redirect_to
From the official Rails guides:
link_to
is for use in ERB templates. It outputs a link to a specific path or url.redirect_to
is for use in controllers. It causes the client to request the specified path or url once the controller method exits.render
is also for use in controllers. It causes Rails to render the specified template.redirect_to
andrender
may only be called once in a given controller method.A link_to creates a hyperlink to a specific URL, which appears on the HTML.
A redirect_to will decide where to link you to, depending on certain options. For example, if someone is logged on as a user, you might want to show him his settings page, else redirect_to the home page.
A render will open the rendered file, take its content and paste it into the existing file, before sending the whole chunk to the recipient.
Hope I am right.
A
link_to
is used on a form within rails, and is a helper to create a link element within html. The other two are not used in forms, but rather are used in controllers.You
render
a page if your controller method is linked to that page. E.g. calling 'new' should render the 'new item' page. They fulfil the request that has just been made.redirect
is used for exactly that - redirecting. For example, if you try accessing a page where you have to be logged in, you redirect the user to the login page. So, redirects basically spawn a new request.