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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:04:48+00:00 2026-06-05T07:04:48+00:00

So I have a Carts model which has_many Items. When I’m on the Carts#show

  • 0

So I have a Carts model which has_many Items. When I’m on the Carts#show page I want to have a link to an action that removes an item from that cart, in other words something that will find the item and set its cart_id to nil.

What should I name this action? Which controller should it be in — CartsController or ItemsController? And lastly… how do I specify this route in my routes.rb (CODE PLEASE FOR THIS ONE)

Edit

I should be a bit more explicit: I don’t want to destroy an item when I remove it from a person’s cart. Each item is unique, and thus destroying it would destroy the listing… which is undesirable. Note that this also implies that this isn’t a HABTM relationship… items can’t belong to more than one cart.

  • 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-05T07:04:50+00:00Added an answer on June 5, 2026 at 7:04 am

    Route:

    resources :carts do
      resources :items
    end
    

    This generates the following routes:

    cart_items GET    /carts/:cart_id/items(.:format)          items#index
                   POST   /carts/:cart_id/items(.:format)          items#create
     new_cart_item GET    /carts/:cart_id/items/new(.:format)      items#new
    edit_cart_item GET    /carts/:cart_id/items/:id/edit(.:format) items#edit
         cart_item GET    /carts/:cart_id/items/:id(.:format)      items#show
                   PUT    /carts/:cart_id/items/:id(.:format)      items#update
                   DELETE /carts/:cart_id/items/:id(.:format)      items#destroy
             carts GET    /carts(.:format)                         carts#index
                   POST   /carts(.:format)                         carts#create
          new_cart GET    /carts/new(.:format)                     carts#new
         edit_cart GET    /carts/:id/edit(.:format)                carts#edit
              cart GET    /carts/:id(.:format)                     carts#show
                   PUT    /carts/:id(.:format)                     carts#update
                   DELETE /carts/:id(.:format)                     carts#destroy
    

    So you’re really explicit in describing items – carts relationship.

    You use your controllers like you normally would.

    I don’t know why would you want to set the cart_id to nil instead of removing it, but this is how you would do it:

    # in items_controller.rb
    
    def destroy
        @item = Item.find(params[:id])
        @item.update_attributes(:cart_id => params[:cart_id])
    
        respond_to do |format|
          format.html { redirect_to items_url }
          format.json { head :no_content }
        end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following cart model which contains many line items. It contains only
I have a rails app (rails 3.1.3) that has a shopping cart model. I
Can anyone give me the 411 on shopping carts? I have a client that
I have a cart table which has a relation to a product table that
I have a Django model for Cat which has a foreign key Dog .
I have a users_manager engine which has a User model class. In an other
I have a cart model class that has a List property like so: public
In my Brain model I have an NSMutableArray property called stockCars which holds 8
I currently have a stored procedure in which I want to insert new rows
I have this href defined in my markup: foreach (Car car in Model.Cars) {

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.