On Thu, Apr 24, 2014 at 10:08 PM, Sebastjan Hribar
<sebastjan.hri...@gmail.com> wrote:
> Hi,
>
> I have a question about duplicating a html form in my views.
>
> To create a new review form in the app I've composed a html form which is a
> table and I've ended up with 200+ lines of code due to table row and data
> blocks.
>
> This is the "input" review form for a user to fill out and submit.
>
> My question is about editing an already existing review form. Is there any
> way of avoiding the duplication of the html form and reusing the existing
> html form? As I understand the inly difference in the edit view is that I
> explicitly call the object attributes from the db and represent them in the
> input fields. If I can't avoid the duplication I'll end up with another 200+
> lines and I should probably then separate my models, views and controllers
> into separate files. Right?

This is a common pattern:

  class PostsNew
    def get
      @post = Post.new # new, empty Post-object
      render :new
    end
  end

  class PostsNEdit
    def get(id)
       @post = Post.find(id)
       render :edit
    end
  end

  module App::Views
    def edit
      form :action => R(PostsNEdit), :method => :post do
        _post_fields
      end
    end

    def new
      form :action => R(Posts), :method => :post do
        _post_fields
      end
    end

    def _form
      input :name => :title, :value => @post.title
    end
  end
_______________________________________________
Camping-list mailing list
Camping-list@rubyforge.org
http://rubyforge.org/mailman/listinfo/camping-list

Reply via email to