form_for(@non_arb)
In an erb template, you can have your form written as:
form_for(@arb)
where @arb is a ActiveRecord::Base object. form_for will smartly realize which url to generate based-on @arb’s state. For example, if I have
form_for(@person)
in the template and @person is a new record, the form_for helper will correctly infer that this form is for creating, thus generates the form submission url as people_path with POST. And if @person is not a new record, the helper will generate the url as person_path(@person) with a “PUT”. Benefits you can get from this are less code, and you can even merge new and edit forms all together.
In one instance, we want to use Jay Fields’ Presenter Pattern. So instead of @person, we use @person_presenter, and we still want to keep the simple syntax on the view, like form_for(@person_presenter)
. To make it work, we need to do the followings:
In your PersonPresenter class, we need to forward new_record? and to_param to person, something like this, or just use Ruby’s Forwardable.
def new_record?
person.new_record?
end
def to_param
person.to_param
end
Now the form_for method will generate person_presenters_path and person_presenter_path respectively. So we need to add two routes in routes.rb:
map.person_presenters 'people', :controller => 'people', :action => 'create', :conditions => { :method => :post }
map.person_presenter 'people/:id', :controller => 'people', :action => 'update', :conditions => { :method => :put }
Now the system works just fine.
[…] on form_for(@non_arb) My previous post talked about how to make form_for(@non_arb) work. When we need it for multiple classes, we came up […]
More on form_for(@non_arb) « Dynamic and Concise said this on March 14, 2008 at 7:59 am |