How to pack before_filter in some_module.rb file?

I have Module.rb file with code

    module Finder
      module InstanceMethods
        def my_func
          @post = Post.find(params[:id])
        end
      end

      def self.included(base)
        base.send :include, InstanceMethods
        base.before_filter :my_func
      end
    end

And Controller with code

include Finder

But my app crashes with code

    undefined method `name' for nil:NilClass
    Extracted source (around line #5):

    2: 
    3: <p>
    4:   <b>Name:</b>
    5:   <%= @post.name %>
    6: </p>
    7: 
    8: <p>

It seems like @post didn't working. Whats wrong?

Answers


I did it. My module didn't work 'cause I didn't add line

config.autoload_paths += Dir["#{config.root}/lib/**/"] 

into application.rb file.After that, I made my task working with this code:

module Finder
  def self.included(base)
     base.class_eval do
       before_filter :my_func, :only => [:show, :edit, :update, :destroy]
     end
  end
  def my_func
       @post = Post.find(params[:id])
  end

Need Your Help

Excel to MySQl but host won't support remote addresses

mysql excel

I have an working Excel file, with file I can send data extracted from an drawing program to an remote MySQL server. Since last week my host has canceled the option to send data from an remote host...

Dynamically creating Expression Blend controls in WPF C#

c# wpf wpf-controls expression-blend

I have created a button in Expression Blend 4. I want to dynamically create instances of this button at run time.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.