This is the first post.
There was a time when the difference between new and create was confused in the review I will post it as a memorandum.
Both mean create. But there are two major differences.
The first is whether to give a ```id `` `
No ** ID is given to the new action. ** ** Also, it often transitions to a page dedicated to new actions. For example, if it is a new post of Tweet or Mercari, it is better to understand it as an exhibition page.
Conversely, the create action ** gives an ID to the post ** For example, if you post on Tweet, the poster and time of posting at that time will be listed. This is given an ID at the moment of posting and is saved in the DB from the model. From here, you can edit or delete it, but these are only possible because you have an ID.
sample.rb
Basic usage of action controller
new action
def new
@tweet = Tweet.new
end
create action
def create
Tweet.create(tweet_params)
end
The second is `` `, which has a different HTTP method. You can find it by checking rake routes (or rails routes) in the terminal.
create action
tweets POST /tweets(.:format) tweets#create
new action
new_tweet GET /tweets/new(.:format) tweets#new
There are four types of HTTP methods. See below.
HTTP method When is the request used?
When only the operation to display the GET page is performed
When performing an operation to register POST data
When performing operations to change PUT data
When performing an operation to delete DELETE data
That is all.
Recommended Posts