Topic: Moving from the view to the controllers

Hi there! It's not a whole lot of code that's getting in the way, but I was trying to do something that should be in the controller, but could only get it working in the view. Just want to learn how to make this a bit DRYier for future knowledge:

Current Code:

<% if User.find_by_username(session[:username]).id == article.user_id %>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article, :confirm => 'Are you sure?', :method => :delete %></td>
<% end %>

What I use for other things (as learned here: http://railsforum.com/viewtopic.php?pid=81478 )

<% if admin? %>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article, :confirm => 'Are you sure?', :method => :delete %></td>
<% end %>

And in the app/controllers/application.rb file:

   def admin?   
       if User.authenticate(session[:username], session[:password])
         true
       else
         false
       end
   end

All of this would be pretty simple, the only problem is for this one I need to pass an array variable into the true/false function (article.user_id is in the index page of a scaffolded controller, listed out in the default table,) which I usually don't need. I know it isn't a whole lot of code, but this seems like some basic ruby functionality that I can't figure out. Thanks!

Re: Moving from the view to the controllers

I'm not sure what your problem is exactly - do you just want to find out if the current user is the article's author?

presumably you have this association in article.rb

belongs_to :user

Then, you just need a method in application rb to return the current user

#in application.rb

def current_user
  User.find_by_username(session[:username])
end

now in your view you can say

<% if current_user == article.user %>

There's various different ways to do this, but this is the most readable i think.

Last edited by Max Williams (2008-12-18 10:01:29)

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: Moving from the view to the controllers

btw, i didn't understand what you wanted to do with the array variable, but you can make arguments optional by giving them a default value, eg

   def admin?(user = current_user)   
     user.role == "admin"
   end

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: Moving from the view to the controllers

Your :admin? method can be written as

def admin?
  User.authenticate( session[ :username ], session[ :password ] )
end

I'll let you figure out why.

As for your question, it's not at all clear what you're after, but I'd guess at the same thing Max did, so I won't re-answer that.

If I've helped you out with something, feel free to recommend me on working with rails.