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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With