rest - Difference between new and create (or edit and update) Rails -


i know question has been asked many times on stackoverflow, have more specific question:

i know new used create form , doesn't save nothing on db. instead, create action saves , validates data on db.

let's see these:

  def new     @country = country.new   end     def create     @country = country.new(params[:country])         respond_to |format|       if @country.save               format.html { redirect_to countries_index_path, notice: 'provincia creata.' }         format.json { render :json => countries_index_path, :status => :created, :location => @country }       else         format.html { render :action => "new" }         format.json { render :json => @country.errors, :status => :unprocessable_entity }       end     end   end 

i want know , why framework doesn't permit use single variable , passed new create handle creation of resource? mean , in new , create create every time 2 different variable, country.new: use 1 create form , , other pass data of form country.new(params[:country]). or better , why can't collide 2 action new , create 1 action? (for restfull theory can't think). should seems stupid question want have clear concept in mind.

thank you.

why framework doesn't permit use single variable , passed new create handle creation of resource

well, many reasons. 1 variables not identical. creates blank country render empty form:

@country = country.new 

while 1 part 1 of two-step process (new+save). creates country object from submitted data.

@country = country.new(params[:country]) 

or better , why can't collide 2 action new , create 1 action?

it's possible implement such action. tell me, how differentiate between "i want render blank form, not saving anything" , "i want save empty object, taking default values"? not mention, have branch in view, separating 2 distinct logical states ("new form" , "page object has been created")


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -