hi, guys,
 I would like to define a common method in the application controller
because the other resources will use this method.

Scenario: I have 2 resources: Posts (for blogs) and Coupons.

Common method: is_owner_or_admin( object )

I have defined this method within the application controller as I
figured it's akin the base class that the controllers for the
resources will inherit from.
I'm just testing the syntax call so, please don't look at this from a
process design point of view :)

application_controller.rb
=================


class ApplicationController < ActionController::Base
    protect_from_forgery

    def is_owner_or_admin(obj)
      @part = Part.find(params[:id])
      @current_user = get_current_user

       if ( @current_user == nil )
         flash[:notice] = 'Access denied'
         redirect_to root_url
         return false
       elsif ( @current_user.is_admin or (@current_user.login ==
@part.created_by) )
         return true
       else
         flash[:notice] = 'Access denied'
         redirect_to root_url
         return false
       end
    end
end


I'm trying to call this method in each of the before_filter in the
resources  (ie. Posts, Coupons).
So, in posts_controller.rb , I do:


class PostsController < ApplicationController
    before_filter :authenticate_user!,
        :except => [:index, :show, :send_message, :get_subcategories]

     ...

    def show
        @post = Post.find(params[:id])
        :is_owner_or_admin(@post)

        respond_to do |format|
            format.html # show.html.erb
            format.xml  { render :xml => @post }
        end
    end

   .....

end



Anyway, when I run the rails server or run "ruby -w
posts_controller.rb", I get the following error:

/home/ct9a/projects/port_over_to_rails3/app/controllers/
posts_controller.rb:20: syntax error, unexpected '(', expecting
keyword_end
                :is_owner_or_admin(@post)
                                              ^

-- 
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