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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:22:00+00:00 2026-05-14T21:22:00+00:00

I think I am missing something while using the Authlogic gem w/ Rails. To

  • 0

I think I am missing something while using the Authlogic gem w/ Rails. To set the stage I have multiple users and each user can create posts and comments. Upon the display of a post or comment I would like to give the user who created them the option to edit or destroy.

I am successfully using the following code to hide and show elements based on if a user is logged in or not but can’t seem to find out how to only show these links to the actual user who created them…not any user that is logged in.

<% if current_user %>
   <%= link_to 'Edit', edit_question_path(question) %> | 
   <%= link_to 'Destroy', question, :confirm => 'Are you sure?', :method => :delete %>
<% else %>
   <p>nothing to see here</p>
<% end %>

Here is the def of current_user located in the application controller in case I need to change something here.

class ApplicationController < ActionController::Base

  helper :all # include all helpers, all the time
  protect_from_forgery # See ActionController::RequestForgeryProtection for details#  

  helper_method :current_user

  private

  def current_user_session
    return @current_user_session if defined?(@current_user_session)
    @current_user_session = UserSession.find
  end

  def current_user
    return @current_user if defined?(@current_user)
    @current_user = current_user_session && current_user_session.record
  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-14T21:22:00+00:00Added an answer on May 14, 2026 at 9:22 pm

    Try this:

    class ApplicationController < ActionController::Base
    
      # add your methods (eg: current_user etc)
      helper_method :current_user, :logged_in?, :current_user_is_owner?
    
      def init_data
        klass = controller_name.camelize.singularize.constantize #User
        param_key = controller_name.camelize.downcase.singularize.to_sym # :user
        obj = case (action_name.to_sym)
          when :new, :create
            klass.new(params[param_key])
          when :edit, :show, :destroy 
            klass.find(params[:id])
          when :update
            klass.find(params[:id]).tap{|o| o.attributes = params[param_key]}
        end
        instance_variable_set("@#{param_key}", obj) # set the obj to @line_item    
      end
    
      def require_user
        return true if logged_in?
        render_error_message("You must be logged in to access this page", 
            new_user_session_url)
        return false
      end
    
      def require_owner
        obj = instance_variable_get("@#{controller_name.singularize.camelize.underscore}") # LineItem becomes @line_item
        return true if current_user_is_owner?(obj)
        render_error_message("You must be the #{controller_name.singularize.camelize} owner to access this page", root_url)
        return false
      end
    
      def logged_in?
        return current_user != nil 
      end
    
      def current_user_is_owner?(obj)
        logged_in? and obj.respond_to?(:user_id) and 
             (obj.send(:user_id) == current_user.id)    
      end
    
      def render_error_message message, url
        respond_to do |format|
          format.html do
            flash[:notice] = message
            if request.xhr?
              head :bad_request, :error => message
            else
              redirect_to url
            end
          end
          format.json { render :json => message, :status => :unprocessable_entity }
          format.xml { render :xml => message, :status => :unprocessable_entity }
        end    
      end
    
    end
    

    Now in your controller

    class PostsController < ApplicationController
      before_filter :require_user  # all actions require user to be logged in
      before_filter :init_data     # create a member variable called @post, initialized based on the action
      before_filter :require_owner, :only => [:edit, :update, :destroy] #edit, update, and destroy actions require ownership
    
      def update
        if @post.save
        else
        end
      end
    end
    

    In the view code:

    <% if current_user_is_owner?(question) %>
      .. display something
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 454k
  • Answers 454k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer File/@Source provides the location to get information about the file… May 15, 2026 at 9:46 pm
  • Editorial Team
    Editorial Team added an answer Well, since the sample code of the library you provided… May 15, 2026 at 9:46 pm
  • Editorial Team
    Editorial Team added an answer All cookies expire as per the cookie specification, so this… May 15, 2026 at 9:46 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.