PageRenderTime 240ms CodeModel.GetById 143ms RepoModel.GetById 4ms app.codeStats 0ms

/ruby-mode/Ruby on Rails/scaffold/scaffold.restful.basic

http://github.com/ridgetang/snippets
Unknown | 75 lines | 67 code | 8 blank | 0 comment | 0 complexity | 773ec4b41740188950fd8a5d1389e8ce MD5 | raw file
  1. #name : generating a RESTful controller
  2. #key : restful
  3. #group : rails.scaffold
  4. #condition : (rails/controller?)
  5. # --
  6. before_filter :capture_${1:`(singularize-string (rails/cur-res-title))`}
  7. $0
  8. def index
  9. @${1:$(pluralize-string text)} = ${2:`(decamelize-string (singularize-string (rails/cur-res-title)))`}.all
  10. respond_to do |format|
  11. format.html # index.html.erb
  12. format.xml { render :xml => @${1:$(pluralize-string text)} }
  13. end
  14. end
  15. def show
  16. respond_to do |format|
  17. format.html # show.html.erb
  18. format.xml { render :xml => @$1 }
  19. end
  20. end
  21. def new
  22. respond_to do |format|
  23. format.html # new.html.erb
  24. format.xml { render :xml => @$1 }
  25. end
  26. end
  27. def edit
  28. end
  29. def create
  30. @$1.update_attributes!(params[:$1])
  31. flash[:notice] = t('.flash')
  32. respond_to do |format|
  33. format.html { redirect_to($1_path(@$1)) }
  34. format.xml { render :xml => @$1, :status => :created, :location => $1_url(@$1) }
  35. end
  36. rescue ActiveRecord::RecordInvalid
  37. respond_to do |format|
  38. format.html { render :action => "new" }
  39. format.xml { render :xml => @$1.errors, :status => :unprocessable_entity }
  40. end
  41. end
  42. def update
  43. @$1.update_attributes!(params[:$1])
  44. flash[:notice] = t('.flash')
  45. respond_to do |format|
  46. format.html { redirect_to($1_path(@$1)) }
  47. format.xml { head :ok }
  48. end
  49. rescue ActiveRecord::RecordInvalid
  50. respond_to do |format|
  51. format.html { render :action => "edit" }
  52. format.xml { render :xml => @$1.errors, :status => :unprocessable_entity }
  53. end
  54. end
  55. def destroy
  56. @$1.destroy
  57. flash[:notice] = t('.flash')
  58. respond_to do |format|
  59. format.html { redirect_to(${1:$(pluralize-string text)}_path) }
  60. format.xml { head :ok }
  61. end
  62. end
  63. private
  64. def capture_$1
  65. @$1 = $2.find(params[:id]) if params[:id]
  66. @$1 ||= $2.new
  67. end