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 8157021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:08:20+00:00 2026-06-06T17:08:20+00:00

While learning Rails, I found this example in the Sam Ruby’s book: app/controllers/line_items_controller.rb def

  • 0

While learning Rails, I found this example in the Sam Ruby’s book:

app/controllers/line_items_controller.rb

def create
        @cart = current_cart
        product = Product.find(params[:product_id])
        @line_item = @cart.line_items.build
        @line_item.product = product

        respond_to do |format|
          if @line_item.save
            # this following line is strange for me 
            format.html { redirect_to(@line_item.cart, :notice => 'Line item was successfully created.') }
            format.xml  { render :xml => @line_item, :status => :created, :location => @line_item }
          else
            format.html { render :action => "new" }
            format.xml  { render :xml => @line_item.errors,
              :status => :unprocessable_entity }
          end
        end
  end

Why isn’t the section with redirect_to(@line_item.cart,...) merely replaced by redirect_to(@cart,...) ?

Indeed, @cart is an accessible instance variable.

Are we forced to use @line_item to retrieve cart in this example ?

  • 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-06-06T17:08:21+00:00Added an answer on June 6, 2026 at 5:08 pm

    Is there some sort of nested route involved in this example?
    You could simply do a redirect_to @cart the example should still work.

    Try it out. But it should work from my understanding. Maybe the author wanted to be a little bit more expressive in the whose cart he redirects to.

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

Sidebar

Related Questions

While learning on anonymous methods, i've found the following example on the internet: namespace
While learning an example from learn-you-a-haskell which right triangle that has integers for all
I am developing a small app while learning Android. The app is basically making
I've slightly modified the iPhone SDK's GLSprite example while learning OpenGL ES and it
So I found some similarities between arrays and set notation while learning about sets
Sorry since this question is specific to my problem. While learning reflections i did
While learning .net (by c#) i found 5 ways for checking equality between objects.
I stumbled on this command while learning AJAX. The guy who made the tutorial
I am beginner to freeswitch.While learning this I have seen the terms 'leg A'
I am learning Erlang from the LearnYouSomeErlang web-book. One thing that struck me while

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.