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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:19:31+00:00 2026-05-20T09:19:31+00:00

Being a newbie ROR developer I’ve been thinking of ways of protecting certain methods

  • 0

Being a newbie ROR developer I’ve been thinking of ways of protecting certain methods to make sure the correct user is updating their own content. Here is an example of my approach.

Would you recommend a cleaner way or better way of doing such tasks?

# Example Controller 
class Owner::PropertiesController < Owner::BaseController

  def index
  end

  etc.....

  def update
    @property = Property.find(params[:id])

    # Check correct owner 
    check_owner(:owner_id => @property.owner_id)

    if @property.update_attributes(params[:property])
      redirect_to([:owner, @property], :notice => 'Property was successfully updated.')
    else
      render :action => "edit"
    end

  end

  def destroy
    @property = Property.find(params[:id])

    # Check correct owner 
    check_owner(:owner_id => @property.owner_id)

    @property.destroy
    redirect_to(owner_properties_url)
  end

  private

  def check_owner p = {}
    if p[:owner_id] != session[:owner_id]
      redirect_to([:owner, @property], :notice => "Property not found.")
    end
  end
  • 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-20T09:19:31+00:00Added an answer on May 20, 2026 at 9:19 am

    You could use a gem like declarative_authorization to do this as well. If you want to do it yourself I would recommend simply DRYing up your code a little bit:

    class Owner::PropertiesController < Owner::BaseController
      before_filter :check_owner, :only => [:update, :destroy]
    
      def update
        if @property.update_attributes(params[:property])
          redirect_to([:owner, @property], :notice => 'Property was successfully updated.')
        else
          render :action => "edit"
        end
      end
    
      def destroy
        @property.destroy
        redirect_to(owner_properties_url)
      end
    
      private
    
      def check_owner
        @property = Property.find(params[:id]
    
        if @property.owner_id != session[:owner_id]
          redirect_to([:owner, @property], :notice => "Property not found.") and return
        end
      end
    end
    

    Additionally, you can filter your properties by an owner to ensure that a user who is not the owner can not interact with properties that aren’t his/hers. For example:

    def update
      @owner = Owner.find(session[:owner_id])
      @property = @owner.properties.find(params[:id])
      redirect_to unauthorized_page and return if @property.nil?
    end
    

    This forces the properties that you are searching to be the ones that belong to the session[:owner_id] instead of the entire universe of properties. This means that properties that the session[:owner_id] does not own will not even be considered. You can then put this code into a before_filter as well so that it’s reusable in multiple actions.

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

Sidebar

Related Questions

Being a newbie to Javascript and jQuery, I am not sure whether this is
Being the 'newbie' I have been staring at the 'documentation and api reference' docs
Being a newbie I thought I understood what to do from a security standpoint
Being a self-confessed newbie I'm looking for some advice and guidance :) I have
I have a question that may sound odd, but being somewhat of a newbie,
Being a Java developer in an agile development cycle, I have learnt that it
I'm creating a Wordpress plugin and, being a newbie in the development on this
def merge(l1,l2): i=0; while((l1[i]!=none)||(l2[i]!=none)): SyntaxError: invalid syntax being a newbie i can't figure out
Very primitive question but I am stuck (I guess being newbie). I have a
being the C# newbie that I undoubtedly am, I'm a bit confused that you

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.