Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
575 views
in Technique[技术] by (71.8m points)

ruby on rails - what is the difference between link_to, redirect_to, and render?

I am confused about the main difference(s) among link_to, redirect_to and render in Rails. anyone can please explain.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

link_to is used in your view, and generates html code for a link

<%= link_to "Google", "http://google.com" %>

This will generate in your view the following html

<a href="http://google.com">Google</a>

redirect_to and render are used in your controller to reply to a request. redirect_to will simply redirect the request to a new URL, if in your controller you add

redirect_to "http://google.com"

anyone accessing your page will effectively be redirected to Google

render can be used in many ways, but it's mainly used to render your html views.

render "article/show"

This will render the view "app/views/article/show.html.erb"

The following link will explain the redirect_to and the render methods more in detail http://guides.rubyonrails.org/layouts_and_rendering.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...