Rails controller create action difference between Model.new and Model.create -
i'm going through few rails 3 , 4 tutorial , come across love insights on:
what difference between model.new , model.create in regards create action. thought use create
method in controller saving eg. @post = post.create(params[:post])
looks i'm mistaken. insight appreciated.
create action using post.new
def new @post = post.new end def create @post = post.new(post_params) @post.save redirect_to post_path(@post) end def post_params params.require(:post).permit(:title, :body) end
create action using post.create
def new @post = post.new end def create @post = post.create(post_params) @post.save redirect_to post_path(@post) end def post_params params.require(:post).permit(:title, :body) end
i have 2 questions
- is rails 4 change?
- is bad practice use
@post = post.create(post_params)
?
model.new
the following instantiate , initialize post model given params:
@post = post.new(post_params)
you have run save
in order persist instance in database:
@post.save
model.create
the following instantiate, initialize and save in database post model given params:
@post = post.create(post_params)
you don't need run save
command, built in already.
more informations on new
here
more informations on create
here
Comments
Post a Comment