Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing controller method attributes in before_filter methods in Rails 3
    primarykey
    data
    text
    <p>I'm trying to only allow access to galleries by users who have permission to view them. Galleries have many shared_users through permissions, and vice versa. Galleries also have only one owner. Owners and shared users are both of the User class. This all works fine. The issue that I'm having, however, is with my access filters. </p> <p>I'm using the following code to see if a user is allowed to see the gallery they are trying to access:</p> <pre><code>def authenticate_viewers! if user_signed_in? &amp;&amp; current_user.can_view?(@gallery) return true end redirect_to root_url, :notice =&gt; "You must have permission to view this gallery." return false end </code></pre> <p>As you can see, <code>can_view?</code> requires the <code>@gallery</code> that I'm setting up in the <code>show</code> method, but a <code>before_filter</code> won't let you access the attributes set up in the method, since it resolves before the method executes. Using an <code>after_filter</code> works, as long as an unauthorized user doesn't try to view the gallery. If it does, I get a DoubleRender error, since <code>after_filter</code> allows the page to render, then tries to redirect.</p> <p>I just thought that I could perhaps use params[:id] instead of @gallery, though I haven't tried it yet, and ultimately this may be more efficient (passing an integer instead of an object). In any case, is there a way to make my current code work? or is it in my best interest to switch to using the params (if that's even going to work)?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload