what is the difference between link_to, redirect_to, and render?

后端 未结 9 820
余生分开走
余生分开走 2020-12-09 08:16

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

9条回答
  •  被撕碎了的回忆
    2020-12-09 08:33

    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

    Google
    

    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

提交回复
热议问题