Thanks Colin..

I am using ternary operator on @currentuser:
@currentuser ? can_admin?(topic) : false .

User model method: topic_admin changed to can_create
Topic model method: can_admin changed to can_be_created_by

@currentuser.can_create?(topic)

### User model has this method:
def can_create?(topic)
  topic.can_be_created_by?(self)
end
----

### Topic model has this method:
def can_be_created_by?(user)
  valid_roles = %w{admin editor}
  if valid_roles.include?(user.role.name)
    return true
  else
    return false
  end
end
---

Problem -
I have nested resources in my application - (topic has_many items). And 
on my show topic page I have a link to 'Add new item'. I would like to 
show this link only to users with certain roles. However, I am not able 
to pass this resource (item) as it has not been initialized. So how can 
I call method like: @currentuser.can_create?(item) from my show topic 
page. I am initializing resource objects within the controller, but how 
can I initialize these from my view? Do I need to? Any clues?

-
CS


Colin Law wrote:
> One solution would be to provide a guest user in the database that is
> returned by current_user when no-one is logged in, that enables you to 
> give
> the guest user whatever roles you desire.  Alternatively test 
> @currentuser
> before calling topic_admin.  As an aside I would suggest setting up
> @topic_admin true or false in controller rather than calling it from 
> view,
> or even better if possible setup the data to be displayed in the
> controller/model and remove the logic from the view entirely.
> 
> 2009/3/11 Carlos Santana <[email protected]>

-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to