render and redirect_to works in the similar way in your web browser , as they both takes you to a new page but the only difference between them is :
- When you call render it creates a complete response which is sent to your browser.
- If we do not define a call to our render , it will simply search for the view of the same name as that of your controller's action name.
- You can also render another view in your controller by simply calling the render and providing it with the action name. eg:render 'profile'
- If you want to render a view to a different controller , you just define the controller name and the action name. eg:render 'users/profile'
- You can even render the arbitrary files.
- render_to_string is a method which does not send a response to the browser , instead it returns a string which tells what is going to be rendered.
Example:
class UsersController < ApplicationController
def update
@user=User.find(params[:id])
if @user.update(user_params)
render 'profile'
end
end
end
2.1 redirect_to will make a different request to the browser to redirect it to the new location.
2.2 It can take you to a completely different location in your website or to some defined location in your application.
2.3 By default rails directs you to 302 http request.
Example:
class UsersController < ApplicationController
def update
@user=User.find(params[:id])
if @user.update(user_params)
redirect_to profile_path
end
end
end
0 Comment(s)