Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access variables from another controller

I've just switched from PHP to rails and I'm trying to figure out how to get my menu_price controller to access a variable from my categories controller.

class CategoriesController < ApplicationController
  before_action :set_category, only: [:show, :edit, :update, :destroy]

  def index
     @categories = Category.all
  end

I thought of global variables but rad that this was not a safe approach. I also read that ruby does not support multiple inheritance which was going to be my second approach.

Here's my menu controller (I only added a portion of it because the rest of the code is not relevant.)

class MenuPricesController < ApplicationController
  before_action :set_menu_price, only: [:show, :edit, :update, :destroy]

  def index
    @menu_prices = MenuPrice.all
  end

I'm sure this is very simple but I've been looking for the past hour or so for an answer. I read about the concern folder but I'm not sure how to approach that, I haven't gotten that far in my book.

like image 686
Paul Brunache Avatar asked Aug 03 '26 19:08

Paul Brunache


1 Answers

In your MenuPricesController you can create a new Category instance and use it like this :

class MenuPricesController < ApplicationController
  before_action :set_menu_price, only: [:show, :edit, :update, :destroy]


 def index
    @menu_prices = MenuPrice.all
    @categories = Category.all
    # do something
  end
like image 139
Ahmad Al-kheat Avatar answered Aug 06 '26 10:08

Ahmad Al-kheat