Skip to content

Instantly share code, notes, and snippets.

@thehenster
Last active November 16, 2015 16:18

Revisions

  1. Henry revised this gist Nov 16, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions recipes_controller.rb
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,7 @@ def create
    def update
    recipe = Recipe.find(params[:id])
    @recipe_form = RecipeForm.new(recipe)

    if @recipe_form.update
    redirect_to recipes_path
    else
  2. Henry revised this gist Nov 16, 2015. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions recipes_controller.rb
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,11 @@ def create
    def update
    recipe = Recipe.find(params[:id])
    @recipe_form = RecipeForm.new(recipe)
    if @recipe_form.update
    redirect_to recipes_path
    else
    render :edit
    end
    end

    private
  3. Henry revised this gist Nov 16, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions recipes_controller.rb
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@ class RecipesController < ApplicationController
    def create
    @recipe_form = RecipeForm.new(Recipe.new)
    @recipe_form.assign_attributes(recipe_params)

    if @recipe_form.save
    redirect_to recipes_path
    else
  4. Henry revised this gist Nov 16, 2015. 1 changed file with 12 additions and 3 deletions.
    15 changes: 12 additions & 3 deletions recipes_controller.rb
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,17 @@
    class RecipesController < ApplicationController
    def create
    recipe_form = RecipeForm.new(Recipe.new)
    recipe_form.assign_attributes(recipe_params)
    recipe_form.save
    @recipe_form = RecipeForm.new(Recipe.new)
    @recipe_form.assign_attributes(recipe_params)
    if @recipe_form.save
    redirect_to recipes_path
    else
    render :new
    end
    end

    def update
    recipe = Recipe.find(params[:id])
    @recipe_form = RecipeForm.new(recipe)
    end

    private
  5. Henry revised this gist Nov 16, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion recipe_form.rb
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ def initialize(recipe)

    def assign_attributes(attributes)
    attributes.each do |k,v|
    public_send("#{attribute}=", v)
    send("#{attribute}=", v)
    end
    end

  6. Henry renamed this gist Nov 16, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. Henry revised this gist Nov 13, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion aaa
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    test
  8. Henry revised this gist Nov 13, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions aaa
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    test
  9. Henry revised this gist Nov 11, 2015. 2 changed files with 30 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions pattern-for-form-objects.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,24 @@
    class RecipeForm
    ATTRIBUTES = [:name, :cooking_time_minutes]

    attr_reader :recipe
    attr_accessor *ATTRIBUTES

    def initialize(recipe)
    @recipe = recipe
    end

    def assign_attributes(attributes)
    attributes.each do |k,v|
    public_send("#{attribute}=", v)
    end
    end

    def save
    ATTRIBUTES.each do |attribute|
    recipe.public_send("#{attribute}=", send(attribute))
    end

    recipe.save
    end
    end
    13 changes: 13 additions & 0 deletions recipes_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    class RecipesController < ApplicationController
    def create
    recipe_form = RecipeForm.new(Recipe.new)
    recipe_form.assign_attributes(recipe_params)
    recipe_form.save
    end

    private

    def recipe_params
    params.require(:recipe).permit(:name, :cooking_time_minutes)
    end
    end
  10. Henry revised this gist Nov 11, 2015. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions pattern-for-form-objects.rb
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,7 @@
    class RecipeForm
    attr_reader :recipe

    def initialize(recipe)
    @recipe = recipe
    end
    end
  11. Henry created this gist Nov 11, 2015.
    2 changes: 2 additions & 0 deletions pattern-for-form-objects.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    class RecipeForm
    end