Reputation: 776
I know that this question has been asked many times on StackOverflow, but i have a more specific question:
I know that new
is used to create a form and it doesn't save nothing on db.
Instead, the create
action saves and validates data on db.
Let's see these:
def new
@country = Country.new
end
def create
@country = Country.new(params[:country])
respond_to do |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 to know , why the framework doesn't permit to use a single variable , passed from new
to create
to handle the creation of a resource?
I mean , in new
and create
we create every time two different variable, Country.new
: we use one to create the form , and the other to pass the data of the form Country.new(params[:country])
.
Or better , why we can't collide the two action new
and create
to one action? (for the Restfull theory we can't I think). It should seems a stupid question but i want to have clear this concept in my mind.
Thank you.
Upvotes: 0
Views: 1917
Reputation: 230336
why the framework doesn't permit to use a single variable , passed from new to create to handle the creation of a resource
Well, many reasons. One is that those variables are not identical. This creates a blank country to render an empty form:
@country = Country.new
While this one is part one of two-step process (new+save
). It creates country object from submitted data.
@country = Country.new(params[:country])
Or better , why we can't collide the two action new and create to one action?
It's possible to implement such action. Tell me, how would you then differentiate between "I want to render a blank form, not saving anything" and "I want to save an empty object, taking all default values"? Not to mention, you would now have to branch in the view, separating two distinct logical states ("new form" and "page for object that has just been created")
Upvotes: 2
Reputation: 1364
These two actions do two completely different things - the one action renders something and never writes s.th. to the DB while the other ones main purpose is not to render s.th. but to write s.th. to the DB.
Of course you can write a whole application with just one single action. But you would end up which a huge cascade of if
s. So you use routing to clear up that mess, and separate what does not belong together.
And the one action is idempotent while the other one is not - which is mirrored by one being a GET
and the other one being a POST
request.
Upvotes: 3
Reputation: 536
The new and create are very much different in their implementation The action new is used to render an empty form.Thus new uses an HTTP GET,because GET request isn't supposed to modify any data. new only creates the local object but does not attempt to validate or save it to the DB.
But in case of create since our aim is to create new data we are using an HTTP POST to the create controller. create instantiates the new object, validates it, and then saves it to the database.
Upvotes: 1