Method deprecated or moved
This method is deprecated or moved on the latest stable version.
The last existing version (v1.2.6) is shown here.
scaffold(model_id, options = {})
public
Adds a swath of generic CRUD actions to the controller. The
model_id is automatically converted into a class name unless one
is specifically provide through options[:class_name]. So scaffold
:post would use Post as the class and
@post/@posts for the instance variables.
It’s possible to use more than one scaffold
in a single controller by specifying options[:suffix] = true. This
will make scaffold
:post, :suffix => true use method names like list_post, show_post,
and create_post instead of just list, show, and post. If suffix is used,
then no index method is added.
Show source
def scaffold(model_id, options = {})
options.assert_valid_keys(:class_name, :suffix)
singular_name = model_id.to_s
class_name = options[:class_name] || singular_name.camelize
plural_name = singular_name.pluralize
suffix = options[:suffix] ? "_#{singular_name}" : ""
unless options[:suffix]
module_eval "def index\nlist\nend\n", __FILE__, __LINE__
end
module_eval "\nverify :method => :post, :only => [ :destroy\#{suffix}, :create\#{suffix}, :update\#{suffix} ],\n:redirect_to => { :action => :list\#{suffix} }\n\n\ndef list\#{suffix}\n@\#{singular_name}_pages, @\#{plural_name} = paginate :\#{plural_name}, :per_page => 10\nrender\#{suffix}_scaffold \"list\#{suffix}\"\nend\n\ndef show\#{suffix}\n@\#{singular_name} = \#{class_name}.find(params[:id])\nrender\#{suffix}_scaffold\nend\n\ndef destroy\#{suffix}\n\#{class_name}.find(params[:id]).destroy\nredirect_to :action => \"list\#{suffix}\"\nend\n\ndef new\#{suffix}\n@\#{singular_name} = \#{class_name}.new\nrender\#{suffix}_scaffold\nend\n\ndef create\#{suffix}\n@\#{singular_name} = \#{class_name}.new(params[:\#{singular_name}])\nif @\#{singular_name}.save\nflash[:notice] = \"\#{class_name} was successfully created\"\nredirect_to :action => \"list\#{suffix}\"\nelse\nrender\#{suffix}_scaffold('new')\nend\nend\n\ndef edit\#{suffix}\n@\#{singular_name} = \#{class_name}.find(params[:id])\nrender\#{suffix}_scaffold\nend\n\ndef update\#{suffix}\n@\#{singular_name} = \#{class_name}.find(params[:id])\n@\#{singular_name}.attributes = params[:\#{singular_name}]\n\nif @\#{singular_name}.save\nflash[:notice] = \"\#{class_name} was successfully updated\"\nredirect_to :action => \"show\#{suffix}\", :id => @\#{singular_name}\nelse\nrender\#{suffix}_scaffold('edit')\nend\nend\n\nprivate\ndef render\#{suffix}_scaffold(action=nil)\naction ||= caller_method_name(caller)\n# logger.info (\"testing template:\" + \"\\\#{self.class.controller_path}/\\\#{action}\") if logger\n\nif template_exists?(\"\\\#{self.class.controller_path}/\\\#{action}\")\nrender :action => action\nelse\n@scaffold_class = \#{class_name}\n@scaffold_singular_name, @scaffold_plural_name = \"\#{singular_name}\", \"\#{plural_name}\"\n@scaffold_suffix = \"\#{suffix}\"\nadd_instance_variables_to_assigns\n\n@template.instance_variable_set(\"@content_for_layout\", @template.render_file(scaffold_path(action.sub(/\#{suffix}$/, \"\")), false))\n\nif !active_layout.nil?\nrender :file => active_layout, :use_full_path => true\nelse\nrender :file => scaffold_path('layout')\nend\nend\nend\n\ndef scaffold_path(template_name)\nFile.dirname(__FILE__) + \"/templates/scaffolds/\" + template_name + \".rhtml\"\nend\n\ndef caller_method_name(caller)\ncaller.first.scan(/`(.*)'/).first.first # ' ruby-mode\nend\n", __FILE__, __LINE__
end