deejay123
deejay123

Reputation: 89

How do I set up my Rails routes for a belongs-to relationship?

So I've got a simple database for an online retail store with a relationship of Users who have many Products through Cartproducts

Users have many cartproducts and have many products through cartproducts. Cartproducts belong to users and products. Products have many cartproducts and have many users through cartproducts.

If that's a bit fudgy to read, here are my models:

class User < ApplicationRecord
  has_many :cartproducts
  has_many :products, through: :cartproducts
end


class Cartproduct < ApplicationRecord
  belongs_to :user
  belongs_to :product
end


class Product < ApplicationRecord
    has_many :cartproducts
    has_many :users, through: :cartproducts
end

What I am trying to do is be able to post to a users cartproducts (ie. add items to that users cart) from the backend, on a route that would look something like: users/1/cartproducts

How would I set up my routes in order to allow a posting/updating/deleting from a users cartproducts? As mentioned earlier, the endpoint would be something like users/:userID/cartproducts. Currently the issue is 'users/1/cartproducts' returns the same cartproducts array as 'users/2/cartproducts'. When I post to either users/1/cartproducts or users/2/cartproducts, both go into the user/1/carproducts.

Edit:

Here are my routes, I'm certain there is a problem here,

Rails.application.routes.draw do
  namespace :api do
    namespace :v1 do
      resources :products
      resources :users, only: [:index, :create, :show] do
        resources :cartproducts
          get '/user_cartproducts', to: "user#cartproducts"
      end
    end
  end
end

Upvotes: 0

Views: 343

Answers (2)

Hackman
Hackman

Reputation: 1729

You should remove the get from your routes, like this:

Rails.application.routes.draw do
  namespace :api do
    namespace :v1 do
      resources :products
      resources :users, only: [:index, :create, :show] do
        resources :cartproducts
      end
    end
  end
end

Now you should have a Cartproducts controller to handle the cart_products. The url where you should post to should like this: /users/1/cartproducts Now the create action from the Cartproducts controller should like:

def create
  @user = User.find(params[:user_id])
  @cartproduct = @user.cartproducts.new(cartproduct_params)
  if @cartproduct.save
    # Do something on success
  else
    # Do something on failure
  end
end

Upvotes: 1

leafeve
leafeve

Reputation: 150

Associations are right First step, you should be create a routes like this (could i see your routes)

   resources :users do
    get :cartproducts, on: :member
   end

and just add action to user_controller.rb

class UsersController < ApplicationController
  before_action :find_user_by_id, only: %I[show cartproducts]

  def show; end

  def cartproducts
    @cartproducts = @user.cartproducts
  end

  private 

  def find_user_by_id
    @user = User.find_by_id(params[:id])
  end
end

Upvotes: 0

Related Questions