link_to、redirect_to 和 render 之间有什么区别? [英] what is the difference between link_to, redirect_to, and render?

查看:53
本文介绍了link_to、redirect_to 和 render 之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Rails 中 link_toredirect_torender 之间的主要区别感到困惑.谁能解释一下.

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

推荐答案

link_to 用于您的视图,并为链接生成 html 代码

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

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

这将在您的视图中生成以下 html

This will generate in your view the following html

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

redirect_to 和 render 在您的控制器中用于回复请求.如果在您的控制器中添加

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"

任何访问您页面的人都会被有效地重定向到 Google

anyone accessing your page will effectively be redirected to Google

render 有多种用途,但主要用于渲染 html 视图.

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

render "article/show"

这将呈现视图app/views/article/show.html.erb"

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

以下链接将更详细地解释redirect_to和render方法http://guides.rubyonrails.org/layouts_and_rendering.html

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

这篇关于link_to、redirect_to 和 render 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆