In Rails, we define the create action 2 ways. What are the difference?
def create
@shop = Shop.new(params[:shop])
if @shop.save
flash[:success] = 'Thanks for adding new shop.'
redirect_to @shop
else
flash[:error] = 'Error adding review, please try again.'
redirect_to @shop
end
end
# or
def create
@shop = Shop.create(params[:shop])
if @shop.save
flash[:success] = 'Thanks for adding new shop.'
redirect_to @shop
else
flash[:error] = 'Error adding review, please try again.'
redirect_to @shop
end
end
Considering we already have:
def new
@shop = Shop.new
end
Which is more proper?
If you used
Model.create, you don’t need to save the object explicitly. The create method will do that for you.If you used
Model.new, you need to save the object by doing @object.save. The newmethod does not do that for you.
Using Model.new:
Using Model.create: