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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:00:42+00:00 2026-05-27T20:00:42+00:00

I am using cancan for authorization. I have a shared view which need authorize

  • 0

I am using cancan for authorization. I have a shared view which need authorize depending on which controller it is.

The problem is:
I have shared partial (description.rhtml) and it is used by two different models (Product and Orders). So when some one go to

http://www.example.com/product/1 – description section shows description about product
http://www.example.com/order/1 – description section shows description about order

This description section has edit button on it so the user can edit it but the condition is

  • the user must be owner of the product when on product/1 page or
  • owner of order when user is on order/1 page.

My ability class check for

  • if user is owner or not – depending on product or order controller
    However on view:

    if (can? :update, @orders) || (can? :update, @product)
    < hide edit button >
    end
    

    but if can? :update, @orders return true or false, it show or hides edit button depending on that condition only

So my question is how can make use CanCan to tackle this problem

Hope I was clear.

  • 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-27T20:00:42+00:00Added an answer on May 27, 2026 at 8:00 pm

    I think you should not use the exact same partial for products and orders.

    You might want to use a layout:

    views/layouts/description.html.erb

    <div class="description">
        <%= model.description %>
        <!-- other common code... -->
        <div class="actions">
            <%= yield %>
        </div>
    </div>
    

    views/orders/description.html.erb

    <%= render :layout => "layouts/description", :locals => { :model => @order } do %>
        <%= if (can? :update, @order) %>
           your link
        <% end %>
    <%= end %>
    

    You don’t have to do that, but I think it’s cleaner than having to deal with several models in the same partial.


    side note:

    but if can? :update, @orders return true or false, it show or hides edit button depending on that condition only

    I don’t really understand that. If @orders were null, then can? would return false, and the result of the whole expression would be the result of (can? :update, @product) which, I thought, was what you wanted.

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

Sidebar

Related Questions

My setup: Rails 3.0.9, Ruby 1.9.2 I am using Cancan to authorize a controller
I'm using cancan as my authorization engine. I already have the roles in user:
I have a problem using a method defined on a model in CanCan abilities:
Using RSpec and Cancan, I have this test which fails with: Failure/Error: User.should_receive(:new).and_return(@user) expected:
I have two entities. User and Role. I am using Devise and CanCan. They
CanCan behaves really strange with index action in controller while using InheritedResouces. If i
I'm trying to get some basic authentication/authorization with devise/cancan with Rails. Rather than using
I'm using Cancan to control User abilities, and have recently run into an odd
I am using check authorization in the application controller so every action will require
Hello I'm actually using Ryan' Bates cancan gem authorization to make my application's users

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.