Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7709069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:46:17+00:00 2026-06-01T00:46:17+00:00

I have a number of existing methods within a controller. Each one of those

  • 0

I have a number of existing methods within a controller. Each one of those methods needs validation.

To do this I have created a validation method within the application controller.

I want to call this validation method from the start of each individual method, which will check that the user has the required groups before continuing. The validation method returns true or false.
If the validation methods returns false, the browser is redirected to a ‘validation failed’ page and the rest of that method is not executed.
The original method is called using JQuery.

I will show some code and then explain what the problem is:

Jquery Ajax Request: (generic code but just to illustrate)

jQuery.ajax({
    url:"examplecontroller/examplemethod/" + ID,
    success: function(data,textstatus,XMLHttpRequest)
    {
    }
})

Method JQuery Request Calls:

def examplemethod
    id = ExampleDatabase.find(params[:id])

    #redirect to validationFail page if validation check returns false#
    render "/validationFail" if !(check_groups(id, "type"))

    ...other code etc...

    render :text => done

end

End Of Validation Method:

def check_groups(id, type)

    ... other code etc ....

    if userGroups.include? requiredGroup then
        return true
    else
        return false
    end
end

Regarding this line which calls the validation method-

render "/validationFail" if !(check_groups(id, "type"))

I did it like this because I just want a short, concise, one-line bit of code that I can just stick at the beginning of each method which either validates, and allows the rest of the method to be executed. Or it doesn’t validate and it stops execution of the rest of the method.

My problem is that having the second ‘render’ line seems to invalidate the first one.
If I have the second ‘render’ line in, the first one does not redirect to the ‘validation fail’ page.
If I take the second validation line out, then there is no response to the JQuery Ajax call (assuming the method validates and is executed fully). This causes an error in Firebug, and obviously my AJAX ‘success’ code is not executed.

I know I could use IF statements to render one or the other, ie:

def examplemethod
    id = ExampleDatabase.find(params[:id])

    if !(check_groups(id, "type")) then
        render "/validationFail"
    else

        ...other code etc...

        render :text => done

    end
end

But I don’t really want to go through and restructure each method like that, I’d rather just use a simple line at the beginning of each method that either returns true and continues, or returns false and doesn’t continue.

It’s strange (to my limited knowledge) that the second render affects the first, seeing as I thought using render would stop subsequent code executing…I’ve looked into using redirect_to instead of the first render, but I couldn’t get this to work.

Can anyone suggest a better way of doing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:46:19+00:00Added an answer on June 1, 2026 at 12:46 am

    I think what you’re looking for here is a before_filter. before_filters allow you to execute code and render content before selected actions, cancelling the action if the filter returns false. The documentation for filters can be found here, but for a piece of example code, you’re likely looking for something like this:

    def TestController < ApplicationController
      before_filter :check_groups
    
      def examplemethod
        id = ExampleDatabase.find(params[:id])
    
        render :text => 'done'
      end
    
      private
    
      def check_groups
        if !(check_groups(id, "type"))
          render '/validationFail'
          return false
        end
      end
    end
    

    Returning false in the before_filter will halt execution of the action, preventing other filters (or the action itself) from being called. If you use this method all over the place, you can even put it in your application controller, and you can throttle which methods before_filter executes on by reading up more on it in the documentation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a number of classes (more than 40), each one has a number
I have a number of textboxes that will be displayed with their existing values.
So I'm working to improve an existing implementation. I have a number of polymorphic
I have a number of classes and they are quite close to each other
I'm currently integrating some new classes into an existing application. I have a JDialog
OK, so I have a couple of modules in my application. One is called
I have a service hosted in a WPF application with an async method with
I'm trying to alter the existing number formatting in my company's application to make
I made this javascript method that I altered from an existing script that I
I have a number of classes that call say Trace.WriteLine(LogSomethingClassSpecific), in various methods all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.