Saturday, March 22, 2008

Generating a resource in the routes.rb file

When making a generator, you may want to add something to the routes.rb file. To understand this you need to know how to make generators. Take a look at Ryan Bates' Railscast on how to do this.

To add a normal resource, do it this way:


class GawingFileableGenerator < Rails::Generator::Base
def manifest
record do |m|
...
m.route_resources :files
end
end
end


If you want to add other stuff like :member or :collection, do it this way


class GawingFileableGenerator < Rails::Generator::Base
def manifest
record do |m|
...
route_string = ":files, :member => { :download => :get }"
def route_string.to_sym; to_s; end
def route_string.inspect; to_s; end
m.route_resources route_string
end
end
end

No comments: