Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3321836
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:06:39+00:00 2026-05-17T23:06:39+00:00

Sorry if the question is obvious, I am only starting to work with Rails.

  • 0

Sorry if the question is obvious, I am only starting to work with Rails.
I have a following code in several controller methods now:

respond_to do |format|
    if @project.save
        format.html { redirect_to(edit_project_url(@project), :notice => '#{user.name} added to #{role}.') }
        format.js
    else
        format.html { render :action => "edit" }
        format.js #...
    end
end

So the question is, what is the best way to do the same thing for errors in all methods?
Is it recommended that I use save! and handle it in rescue_action?

Or should I do my own respond method and pass save in a block?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:06:40+00:00Added an answer on May 17, 2026 at 11:06 pm

    It’s often more convenient to use the exception-raising variant of save and rescue that later in the block than to branch like that. The advantage to exceptions is they’ll bust out of transactions.

    def create
      @project.save!
    
      respond_to do |format|
        format.html { redirect_to(edit_project_url(@project), :notice => '#{user.name} added to #{role}.') }
        format.js
      end
    
    rescue ActiveRecord::RecordInvalid
      respond_to do |format|
        format.html { render :action => "edit" }
        format.js #...
      end
    end
    

    You’ll find that it gets really tricky to wrangle your way out of a pile of nested if statements when trying to save more than one object at a time, but a simple rescue for exceptions will handle it neatly.

    def create
      Project.transaction do
        @project.save!
        @something_else.save!
        @other_stuff.save!
      end
    
      # ...
    rescue ActiveRecord::RecordInvalid
      # ...
    end
    

    If any one of those saves blows up you’ll get an exception. To ensure that all of them are displaying validation errors you might have to call .valid? on each to prime them or you will have those after the failure left untested.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for asking what seems like such an obvious question. I have an adapter
All Sorry if this is an obvious question but does WCF streaming work correctly
(Sorry in advance if the question is stupid or obvious -- I don't have
I am new to SharePoint, sorry if answer to my question is obvious. I
Quick question (and sorry if it's already been asked, I have looked but couldn't
Sorry if this is an obvious question, but I've found surprisingly few references on
Sorry if this is a question with a obvious answer but my knowledge with
I just started using java so sorry if this question's answer is obvious. I
I have the following ember controller that is going to wait for json objects
Sorry if this question seems obvious for everyone, but I am very new to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.