4

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

3 Answers 3

5

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
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

When you point your browser to /posts/new, it renders 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 posts your data to the create action, which actually creates the record in the database.

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

@post.save

in your new action, since that will save a blank record to the database - whether the user completes the form or not. And, you'll probably want to move

@post.user_id = current_user.id

to your create action, since that is where you're actually saving the post to the database.

1 Comment

ok,that means create is method which will be auto called at submit button !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.