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

后端 未结 9 801
余生分开走
余生分开走 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:42

    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.

提交回复
热议问题