lib/generators/scaffold_controller/templates/controller.rb in nested_scaffold-0.2.1 vs lib/generators/scaffold_controller/templates/controller.rb in nested_scaffold-1.0.0

- old
+ new

@@ -1,90 +1,68 @@ class <%= controller_class_name %>Controller < ApplicationController + before_action :set_<%= plural_name %> + before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy] + # GET <%= plural_nested_parent_name %>/1/<%= plural_name %> - # GET <%= plural_nested_parent_name %>/1/<%= plural_name %>.json def index - @<%= nested_parent_name %> = <%= orm_class.find(nested_parent_class_name, "params[:#{nested_parent_id}]") %> <%= "@#{plural_name} = @#{nested_parent_name}.#{plural_name}" %> - - respond_to do |format| - format.html # index.html.erb - format.json { render :json => @<%= plural_name %> } - end end # GET <%= plural_nested_parent_name %>/1/<%= plural_name %>/1 - # GET <%= plural_nested_parent_name %>/1/<%= plural_name %>/1.json def show - @<%= nested_parent_name %> = <%= orm_class.find(nested_parent_class_name, "params[:#{nested_parent_id}]") %> - <%= "@#{singular_name} = @#{nested_parent_name}.#{plural_name}.find(params[:id])" %> - - respond_to do |format| - format.html # show.html.erb - format.json { render :json => @<%= singular_name %> } - end end # GET <%= plural_nested_parent_name %>/1/<%= plural_name %>/new - # GET <%= plural_nested_parent_name %>/1/<%= plural_name %>/new.json def new - @<%= nested_parent_name %> = <%= orm_class.find(nested_parent_class_name, "params[:#{nested_parent_id}]") %> <%= "@#{singular_name} = @#{nested_parent_name}.#{plural_name}.build" %> - - respond_to do |format| - format.html # new.html.erb - format.json { render :json => @<%= singular_name %> } - end end # GET <%= plural_nested_parent_name %>/1/<%= plural_name %>/1/edit def edit - @<%= nested_parent_name %> = <%= orm_class.find(nested_parent_class_name, "params[:#{nested_parent_id}]") %> - <%= "@#{singular_name} = @#{nested_parent_name}.#{plural_name}.find(params[:id])" %> end # POST <%= plural_nested_parent_name %>/1/<%= plural_name %> - # POST <%= plural_nested_parent_name %>/1/<%= plural_name %>.json def create - @<%= nested_parent_name %> = <%= orm_class.find(nested_parent_class_name, "params[:#{nested_parent_id}]") %> - <%= "@#{singular_name} = @#{nested_parent_name}.#{plural_name}.build(params[:#{singular_name}])" %> + <%= "@#{singular_name} = @#{nested_parent_name}.#{plural_name}.build(#{singular_name}_params)" %> - respond_to do |format| - if @<%= singular_name %>.save - format.html { redirect_to([@<%= singular_name %>.<%= nested_parent_name %>, @<%= singular_name %>], :notice => '<%= human_name %> was successfully created.') } - format.json { render :json => @<%= singular_name %>, :status => :created, :location => [@<%= singular_name %>.<%= nested_parent_name %>, @<%= singular_name %>] } - else - format.html { render :action => "new" } - format.json { render :json => @<%= singular_name %>.errors, :status => :unprocessable_entity } - end + if @<%= singular_name %>.save + redirect_to([@<%= singular_name %>.<%= nested_parent_name %>, @<%= singular_name %>], notice: '<%= human_name %> was successfully created.') + else + render action: 'new' end end # PUT <%= plural_nested_parent_name %>/1/<%= plural_name %>/1 - # PUT <%= plural_nested_parent_name %>/1/<%= plural_name %>/1.json def update - @<%= nested_parent_name %> = <%= orm_class.find(nested_parent_class_name, "params[:#{nested_parent_id}]") %> - <%= "@#{singular_name} = @#{nested_parent_name}.#{plural_name}.find(params[:id])" %> - - respond_to do |format| - if @<%= singular_name %>.update_attributes(params[:<%= singular_name %>]) - format.html { redirect_to([@<%= singular_name %>.<%= nested_parent_name %>, @<%= singular_name %>], :notice => '<%= human_name %> was successfully updated.') } - format.json { head :ok } - else - format.html { render :action => "edit" } - format.json { render :json => @<%= singular_name %>.errors, :status => :unprocessable_entity } - end + if @<%= singular_name %>.update_attributes(<%= singular_name %>_params) + redirect_to([@<%= singular_name %>.<%= nested_parent_name %>, @<%= singular_name %>], notice: '<%= human_name %> was successfully updated.') + else + render action: 'edit' end end # DELETE <%= plural_nested_parent_name %>/1/<%= plural_name %>/1 - # DELETE <%= plural_nested_parent_name %>/1/<%= plural_name %>/1.json def destroy - @<%= nested_parent_name %> = <%= orm_class.find(nested_parent_class_name, "params[:#{nested_parent_id}]") %> - <%= "@#{singular_name} = @#{nested_parent_name}.#{plural_name}.find(params[:id])" %> @<%= singular_name %>.destroy - respond_to do |format| - format.html { redirect_to <%= nested_parent_name %>_<%= index_helper %>_url(<%= nested_parent_name %>) } - format.json { head :ok } - end + redirect_to <%= nested_parent_name %>_<%= index_helper %>_url(@<%= nested_parent_name %>) end + + private + # Use callbacks to share common setup or constraints between actions. + def set_<%= plural_name %> + @<%= nested_parent_name %> = <%= orm_class.find(nested_parent_class_name, "params[:#{nested_parent_id}]") %> + end + + def set_<%= singular_table_name %> + <%= "@#{singular_name} = @#{nested_parent_name}.#{plural_name}.find(params[:id])" %> + end + + # Only allow a trusted parameter "white list" through. + def <%= "#{singular_table_name}_params" %> + <%- if attributes_names.empty? -%> + params.fetch(:<%= singular_table_name %>, {}) + <%- else -%> + params.require(:<%= singular_table_name %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>) + <%- end -%> + end end