Difference between new and create in Rais action controller

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.

Difference between controller actions new and create

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

Difference between new and create in Rais action controller
[Rails] Difference between create method and new + save method
Difference between Thymeleaf @RestController and @Controller
Difference between final and Immutable in Java
Difference between pop () and peek () in stack
Difference between getText () and getAttribute () in Selenium
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
Difference between EMPTY_ELEMENTDATA and DEFAULTCAPACITY_EMPTY_ELEMENTDATA in ArrayList
Difference between int and Integer in Java
[Rails] I studied the difference between new method, save method, build method and create method.
[Rails / ActiveRecord] About the difference between create and create!
Difference between next () and nextLine () in Java Scanner
Difference between vh and%
Difference between i ++ and ++ i
About the difference between classes and instances in Ruby
[Java] Difference between static final and final in member variables
Difference between product and variant
Difference between redirect_to and render
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between puts and print
Difference between redirect_to and render
Difference between CUI and GUI
Difference between variables and instance variables
Relationship between Controller and View
Difference between mockito-core and mockito-all
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
About the difference between "(double quotation)" and "single quotation" in Ruby
Jersey --What is Difference Between bind and bindAsContract in HK2?
Difference between element 0, null and empty string (check in list)
What is the difference between an action and an instance method?
Is short-circuit evaluation really fast? Difference between && and & in Java
[Ruby] Difference between get and post
Difference between render method and redirect_to
Difference between interface and abstract class
Difference between == operator and equals method
[Java] Difference between Hashmap and HashTable
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
[Ruby] Difference between is_a? And instance_of?
Rough difference between RSpec and minitest
Create a new app in Rails
[Rails] Difference between find and find_by
Understand the difference between each_with_index and each.with_index
Difference between instance variable and class variable
[JAVA] Difference between abstract and interface
Difference between Stream map and flatMap
[Java] Difference between array and ArrayList
Difference between primitive type and reference type
Install Rails in the development environment and create a new application
Difference between byCharWrapping and byWordWrapping of UI Label in Japanese display
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
[Java] Difference between Closeable and AutoCloseable