Create action on rails

When I use scaffold in rails, the controller creates various methods, such as

new, create, show, index, etc.

but here I can’t understand the transition of a new action to create an action

eg. when I click on a new message, it searches for a new action, now it processes _form, but when at the time of sending, how was the data entered into this particular table , where is the action of the controller called and how?

My post_controller matches

def new @post = Post.new @post.user_id = current_user.id @post.save respond_to do |format| format.html # new.html.erb format.json { render json: @post } end end # GET /posts/1/edit def edit @post = Post.find(params[:id]) authorize! :manage, @post end # POST /posts # POST /posts.json def create @post = Post.new(params[:post]) respond_to do |format| if @post.save format.html { redirect_to @post, notice: 'Post was successfully created.' } format.json { render json: @post, status: :created, location: @post } else format.html { render action: "new" } format.json { render json: @post.errors, status: :unprocessable_entity } end end end 
+4
source share
3 answers

Default scaffolding in shape ( read here )

When the user clicks the Create message button in this form, the browser will send the information back to the controller creation action (Rails knows to invoke the create action because the form is sent with an HTTP POST request; this is one of the conventions that were mentioned earlier):

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

If you want to configure the action for a new sketch view, you must add :url => {:action => "YourActionName"} in your form.

Example:

 #form form_for @post, :url => {:action => "YourActionName"} #controller def YourActionName @post = Post.new(params[:post]) respond_to do |format| if @post.save format.html { redirect_to(@post, :notice => 'Post was successfully created.') } format.json { render :json => @post, :status => :created, :location => @post } else format.html { render :action => "new" } format.json { render :json => @post.errors, :status => :unprocessable_entity } end end end #route match '/posts/YourActionName`, 'controllers#YourActionName', :via => :post 
+5
source

All about HTTP verbs and routes.

Your form will send a POST request to the /posts route. If you specify your routes using rake routes , you will see that all POST requests for that particular route are sent to the create action in PostsController or posts#create for short.

+1
source

When you point the browser to /posts/new , it displays the new action, which presents you with a form to fill out (defined in app/views/posts/new.html.erb and app/views/posts/_form.html.erb ). When you click the submit button in your form, it sends your data to the create action, which actually creates a record in the database.

Looking at your PostsController code, you probably don't want to have a line

 @post.save 

in your action, new , as this will save an empty record in the database - whether the user will fill out the form or not. And you probably want to move

 @post.user_id = current_user.id 

to your create action, since it is you who actually save the message in the database.

+1
source

Source: https://habr.com/ru/post/1486453/


All Articles