render dựng view ngay trong request hiện tại — không tạo request mới, URL giữ nguyên, instance variables vẫn còn.redirect_to gửi HTTP 302, bảo browser tạo request mới đến URL khác — controller hiện tại kết thúc, action mới chạy từ đầu.
Lỗi kinh điển: dùng render sau POST tạo record → F5 submit lại form (double-submit). Quy tắc PRG (Post → Redirect → Get): sau POST thành công luôn dùng redirect_to.
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post, notice: "Tạo thành công!"
else
render :new, status: :unprocessable_entity # giữ lại lỗi validation
end
endrender builds the view within the current request — no new request, URL stays the same, instance variables remain intact.redirect_to sends an HTTP 302, telling the browser to make a new request to another URL — the current controller finishes, and the new action starts from scratch.
Classic mistake: using render after a successful POST → refreshing resubmits the form (double-submit). PRG rule (Post → Redirect → Get): after a successful POST always use redirect_to.
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post, notice: "Created successfully!"
else
render :new, status: :unprocessable_entity # preserve validation errors
end
end