Mr.D
Mr.D

Reputation: 7873

Rails_admin, change values of one field by value of another

I have three models: Category, SubCategory and Item.

SubCategory belongs to Category

Item belongs to Category and SubCategory

In rails_admin's create and edit page, where I can assign values for items, values of categories and subcategories are shown as enums of ALL existing Categories and SubCategories.

When I create new Item, I will select the Category. I want to keep only values of SubCategories, which is belongs to selected category, and only be able to select them.

How it is possible to make rails_admin to select SubCategories values depending on what Category is selected?

Upvotes: 2

Views: 2663

Answers (1)

bigsolom
bigsolom

Reputation: 2349

you can use the bindings hash to get access the current record instance

The field declarations also have access to a bindings hash which contains the current record instance in key :object and the view instance in key :view. Via :object we can access other columns' values and via :view we can access our application's view helpers

From: https://github.com/sferik/rails_admin/wiki/Fields

so in your case for example, in your Item rails_admin configuration

bindings[:object].category

will give you the current selected category for the item

Upvotes: 2

Related Questions