Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create action in rails

When i use scaffold in rails , controller creates various methods like

new,create,show,index etc

but here i can't understand transition of new action to create action

eg. when i click on new Post it look up for new action,now it render _form,but when at time of submit how data entered to that particular table, where the create action of controller called and how ?

My posts_controller is as

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
like image 583
Akash Shinde Avatar asked Mar 23 '23 13:03

Akash Shinde


2 Answers

By default scaffolding on form (read here)

When the user clicks the Create Post button on this form, the browser will send information back to the create action of the controller (Rails knows to call the create action because the form is sent with an HTTP POST request; that’s 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 customize an action on new form scaffold, you should add :url => {:action => "YourActionName"} on 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
like image 67
rails_id Avatar answered Apr 19 '23 19:04

rails_id


It's all about HTTP verbs and routes.

Your form will make a POST request to the /posts route. If you list your routes using rake routes, you'll see that all POST requests to that specific route are being directed to the create action in PostsController, or posts#create for short.

like image 40
Marcelo De Polli Avatar answered Apr 19 '23 18:04

Marcelo De Polli