Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Redirect_to and Render methods in Rails

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 666
    Comment on it

    As we know that in rails we have models view and their controllers which act as a medium between model and view.

    The normal flow between a controller and a view is to display a view corresponding to its controller action. 

    So this can be achieved in two different ways that is :

    • render
    • redirect_to

    Render

    In rails the render method is called in the controller action where it creates a full response and sends the control the view page specified in render. Render method renders the view with all the instance variables present in the action.

    Render can be used in two different ways. Render is called by default if we don't mention any render action in the controller method and the same controller method's view is rendered by default. For example

    class UsersController < ApplicationController
        def show
          user = User.find(params[:id])
        end
    end

    in this case automatically the show page of the users controller would be rendered as we have not passed any thing in the render method.

    But in case if we want to render something else to an entirely new page after the controller action is called then we have to mention it in the render method. Render never makes an HTTP request. It would just render the page as it is. For example

    class UsersController < ApplicationController
      def update
        @user = User.find(params[:id])
        if @user.update_attributes(params[:user])
          redirect_to(@user)
        else
          render :action => :edit
        end
      end
    end

    In this case if the user is not successfully updated and some error comes up so the edit page will be rendered again.

    Redirect_to

    The redirect method is used to redirect a controller action to a particular webpage or web URL or an entirely different website.

    The redirect method invokes an HTTP call to the web browser and makes a new request.

    Then the web browser sends a response back to the page and renders the page.

    class UsersController < ApplicationController
      def update
        @user = User.find(params[:id])
        if @user.update_attributes(params[:user])
          redirect_to user_path
        end
      end
    end

    Here the user will be redirected to the action corresponding to the user_path URL. It will create a new request and send a response back to the user page.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: