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 266519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:27:05+00:00 2026-05-11T23:27:05+00:00

Basically I want to implement a simple Rails extension to define the seriousness of

  • 0

Basically I want to implement a simple Rails extension to define the seriousness of methods in my controller so that I can restrict usage of them appropriately. For example I’d define the default restful actions as so in an abstract superclass:

view_methods :index, :show
edit_methods :new, :create, :edit, :update
destroy_methods :destroy

I’d then down in a non-abstract controller call:

edit_methods :sort

to add in the sort method on that particular controller as being an edit level method.

I could then use a before_filter to check the level of the action currently being performed, and abort it if my logic determines that the current user can’t do it.

Trouble is, I’m having trouble working out how to set up this kind of structure. I’ve tried something like this so far:

class ApplicationController

  @@view_methods = Array.new
  @@edit_methods = Array.new
  @@destroy_methods = Array.new

  def self.view_methods(*view_methods)
    class_variable_set(:@@view_methods, class_variable_get(:@@view_methods) << view_methods.to_a)
  end

  def self.edit_methods(*edit_methods)
    class_variable_set(:@@edit_methods, self.class_variable_get(:@@edit_methods) << edit_methods.to_a)
  end

  def self.destroy_methods(*destroy_methods)
    @@destroy_methods << destroy_methods.to_a
  end

  def self.testing
    return @@edit_methods
  end


  view_methods :index, :show
  edit_methods :new, :create, :edit, :update
  destroy_methods :destroy

end

The three methods above are different on purpose, just to show you what I’ve tried. The third one works, but returns the same results no matter what controller I test. Probably because the class variables are stored in the application controller so are changed globally.

Any help would be greatly appreciated.

  • 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-05-11T23:27:05+00:00Added an answer on May 11, 2026 at 11:27 pm

    The problem is that your class variables are inherited, but point to the same instance of Array. If you update one, it will also be updated on all classes that inherited the Array.

    ActiveSupport offers a solution to this problem by extending the Class class with several methods to define inheritable class attributes. They are used everywhere internally in Rails. An example:

    class ApplicationController
      class_inheritable_array :view_method_list
      self.view_method_list = []
    
      def self.view_methods(*view_methods)
        self.view_method_list = view_methods  # view_methods are added
      end
    
      view_methods :index, :show
    end
    

    Now you can set default values in ApplicationController and override them later.

    class MyController < ApplicationController
      view_method :my_method
    end
    
    ApplicationController.view_method_list #=> [:index, :show]
    MyController.view_method_list #=> [:index, :show, :my_method]
    

    You can even use the view_method_list as an instance method on the controllers (e.g. MyController.new.view_method_list).

    In your example you didn’t define a way to remove methods from the lists, but the idea is to do something like the following (in case you need it):

    # given the code above...
    class MyController
      self.view_method_list.delete :show
    end
    MyController.view_method_list #=> [:index, :my_method]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is an incredibly simple question (I'm new to Python). I basically want a
Basically I want to pass a string which contains Spanish text that could be
I basically want to use link_to to link to the index method of a
I basically want to do this: grep 'example.com' www_log > example.com.YYYY-MM-DD-H:i:S.log ...with of course
So I basically want to have a static method in my AR class to
I have a 2-dimensional array of objects and I basically want to databind each
Basically I want to know how to set center alignment for a cell using
Basically I want to use RegEx to grab stuff in between paragraphs in a
Basically I want to have our installer also install the .NET framework and any
Basically I want to make sure I will always get the computer's name rather

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.