Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have experienced a situation where "current_user" needs be tightly connected to a model, but I handled it all in the Controller and it works pretty well. Here are some examples:</p> <p>My model is "Photos". Photos are owned by users, and how people interact with photos is obviously tightly related to whether or not they own the photo.</p> <p>In the show action I need to load either the existing rating a user has given to a photo (so they can edit it) or allow them to create a new one:</p> <pre><code>def show @photo = Photo.find(params[:id]) if user_signed_in? if @rating = current_user.ratings.find_by_photo_id(params[:id]) @rating @current_user_rating = @rating.value else @rating = current_user.ratings.new @current_user_rating = "n/a" end end end </code></pre> <p>When people create photos I want them to be automatically assigned to the current user.</p> <pre><code>def new @photo = Photo.new end def create @photo = current_user.photos.create(params[:photo]) if @photo.save redirect_to user_path(current_user), :notice =&gt; t('photo.notice.created') else render 'new' end end </code></pre> <p>Only the owners of a photo can change them:</p> <pre><code>def edit @photo = Photo.find(params[:id]) if @photo.user == current_user render 'edit' else redirect_to user_path(current_user), :alert =&gt; t('application.error.unauthorized') end end def update @photo = current_user.photos.find_by_id(params[:id]) @photo.update_attributes(params[:photo]) if @photo.save redirect_to user_path(current_user), :notice =&gt; t('photo.notice.updated') else render 'edit' end end </code></pre> <p>This approach is based on the constraints that a "current_user" object is tied to the session, which only the controller knows about. So, in short, I have yet to find a good way to integrate "current_user" into a model, but I've been able to find (I think) pretty clean ways to tie the model and controller together so that this can be provided by the controller.</p> <p>One fairly simple solution to most problems, if your controller is starting to get messy, would be to take a chunk of logic and define as a method in the model, but require one argument = a user object. Then you can just feed "current_user" to that method from your controller and the model handles the rest.</p> <p>Good luck! Also, if anyone else has any better ideas for this, I'd love to hear them!</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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