DoubleRenderError in Ruby on Rails

DoubleRenderError in Ruby on Rails

How do I solve the DoubleRenderError by using before_action.To practice my rails skills, I decided to make a website about events. It is a website mainly between organizations, who hold an event and volunteers, who join the event. Among all the problems I encountered, I want to share the DoubleRenderError because it bothered me for a long time and how I solve the problem.

To practice my rails skills, I decided to make a website about events. It is a website mainly between organizations, who hold an event and volunteers, who join the event.

Among all the problems I encountered, I want to share the DoubleRenderError because it bothered me for a long time and how I solve the problem.

Problem

I got this error when I tried to add some helpers method for the show route of User. My application_controller.rb looks like this:

class ApplicationController < ActionController::Base

    def current_user
      User.find_by :id=>session[:user_id]
    end
    def log_in?
      !!session[:user_id]
    end
    def log_in_first
        if !log_in?
          flash[:error]="You have to log in first to continue your               operation"
          redirect_to "/login"
        end
      end
      def correct_user?
        if !([email protected])
         flash[:error]="You have no right to do this operation."
          redirect_to "/"
        end
      end
end

And here’s my show action in users_controller.rb:

def show
   log_in_first
   @user = User.find_by id: params[:id]
   correct_user?
   if @user
      render 'show'
     else
       redirect_to '/login'
   end
end

As you can see, I tried to verify that the user is logged in and it is the correct user before a user can browse on a specific user page. So let’s say if someone’s not logged in but wants to go to the show route, it will be redirected to the login page when it hits the log_in_first method, and see the error message: “You have to log in first to continue your operation”.

However, when I tested it as a not-logged-in user, I got this error:

AbstractController::DoubleRenderError (Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".)

ruby-on-rails error error-handling ruby-on-rails-development ruby

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Explain Ruby on rails MVC

Our Ruby on Rails Training will provide you to learn about Rails and web applications development with realty. Our Ruby on Rails Course also includes live sessions.

Live Updates in Ruby on Rails with ActionCable Feature

There are times when one as a Ruby on Rails developer wants to implement a real-time feature, like a chat application, and after digging a bit you find a framework's feature named ActionCable, sounds new and scary, right? Fear not, I'll try to explain it as simple as possible so that at the end of this article you'll feel comfortable with the subject.

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.

Pros & Cons you must know before using Ruby on Rails for your startup

Click here, free ruby on rails course videos for you. It shows you best way to complete Ruby certification. OnlineITGuru guides you towards easy Web script

Ruby on Rails Performance Tuning

Synchronises Assets between Rails and S3. Contribute to AssetSync/asset_sync development by creating an account on GitHub.