I've generated a scaffold, let's call it scaffold test. Within that scaffold, I've got a _form.html.erb thats being render for action's :new => :create and :edit => :update
Rails does a lot of magic sometimes and I cannot figure out how the form_for knows how to call the proper :action when pressing submit between :new and :edit
Scaffolded Form
<%= form_for(@test) do |f| %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
vs. Un-scaffolded Form
<% form_for @test :url => {:action => "new"}, :method => "post" do |f| %>
<%= f.submit %>
<% end %>
<h1>Editing test</h1>
<%= render 'form' %>
<h1>New test</h1>
<%= render 'form' %>
As you can see theres no difference between the forms How can both templates render the same form but use different actions?
It checks @test.persisted?
If it is persisted then it is an edit form. If it isn't, it is a new form.
It checks if the record is new or not.
@test.new_record? # if true then create action else update action
If the @test
instance variable is instantiated via the Test.new
class method, then the create
method is executed. If @test
is an instance of Test
that exists in the database, the update
method is executed.
In other words:
# app/controllers/tests_controller.rb
def new
@test = Test.new
end
<%= form_for(@test) |do| %>
yields a block that is sent to the create
controller method.
If, instead:
# app/controllers/tests_controller.rb
def edit
@test = Test.find(params[:id])
end
<%= form_for(@test) |do| %>
yields a block that is sent to the update
controller method.
UPDATE:
The precise function that Rails uses to recognize whether or not a record is new is the persisted?
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With