ruby - Why do we declare a @page = Page.new under new action, in Rails? -
this question rails 3.2 , bit more complicated title suggests.
say have pages controller, restful actions. web app can create pages, display, edit , delete existing ones, , list of existing pages.
under new action, have code like
@page = page.new
so, when user visits /pages/new, , fill form_for(@page) do..end stuff, input data saved @page? instance, if title , content page model attributes, @page.title , @page.content return error or display seeking for? or form_for directly save values onto params?
if later case, why declare @page in new action? there form_for infer rails create new page model object rather existing one?
hopefully, wasn't confusing. :p
you can have form class writing
form_for :post
in rails convention make form with
form_for @post
because can render same form
partial new
, create
(on validation errors) edit
, , update
(on validation error) actions. that's why need initialize new "empty" object in new action - keep interface uniform.
Comments
Post a Comment