Skip to content

Working with forms

Jeremy Rodi edited this page Aug 14, 2013 · 2 revisions

There are two ways to deal with forms: if they're simple, just use an ERB partial. If they're complex, handle them with two Curly partials.

Say you have a posts/show view with a comment form. You can break it up like this:

<!-- app/views/posts/show.html.curly -->
<h1>{{post_title}}</h1>

...

{{comment_form}}
<!-- app/views/posts/_comment_form.html.curly -->
{{name_field}}
{{email_field}}
{{comment_field}}

<p>{{submit_button}} or {{cancel_button}}</p>
# app/presents/posts/show_presenter.rb
class Posts::ShowPresenter < Curly::Presenter
  presents :post, :comment

  ...

  # You'll instantiate @comment in the controller.
  def comment_form
    form_for [@post, @comment] do |form|
      render 'comment_form', form: form
    end
  end
end
# app/presenters/posts/comment_form_presenter.rb
class Posts::CommentFormPresenter < Curly::Presenter
  presents :form

  def name_field
    @form.text_field :name
  end

  def email_field
    @form.text_field :email, type: "email"
  end

  def comment_field
    @form.text_area :body
  end

  ...
end
Clone this wiki locally