Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a single form_for work for new, update and edit?

In rails 3, is it possible to create a form_for that works for both new, update and edit actions?

If yes, how do I do this?

I have a admin section so my urls look like:

/admin/posts/ {new,update, edit}
like image 329
Blankman Avatar asked Feb 15 '11 03:02

Blankman


1 Answers

Just let form_for handle it:

http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for-label-Resource-oriented+style

controller:

class ControllerName
  def new
    @post = Post.new
  end
  def edit
    @post = Post.find params[:id]
  end
end

view:

<%= form_for [:admin, @post] do |f| %>
<% end %>
like image 74
james Avatar answered Oct 17 '22 07:10

james