Reputation: 65
I have Active Admin gem for my ruby on rails project. What I would like to do is create a product and have 2 select boxes (1-Category, 2-Department) in that form where the second one changes it's content based on the first one. After countless hours of searching I still can't get this to work.
class Product < ActiveRecord::Base
belongs_to :category, dependent: :destroy, counter_cache: true
has_one :department, through: :category
end
class Category < ActiveRecord::Base
belongs_to :department, counter_cache: true
has_many :products
end
class Department < ActiveRecord::Base
end
The reason I would like to do this is because some departments might have the same category name that would be confusing and would add the product to the wrong department.
I have tried question 9579402 but how I understand his question is that he had only 2 models and he was creating sub-category from selected category
Heres a familiar thing, but he uses a get ajax request Git/Dglgmut/6328501
Trying question 9579402 get me error:
Started POST "/admin/products/change_categories" for ::1 at 2016-03-30 14:51:09 +0300
ActionController::RoutingError (No route matches [POST] "/admin/products/change_categories"):
This is what I have in routes.rb
routes.rb = post 'change_categories' =>'products#change_categories'
http://localhost:3000/rails/info/routes = change_categories_path POST /change_categories(.:format) products#change_categories
I guess this is because I can use Member Actions for Active admin, so I tried it
member_action :change_categories, :method => :get do
@categories = Department.find_by_id(params[:department_id]).try(:categories)
render :text=>view_context.options_from_collection_for_select(@categories, :id, :category_number)
end
But got the same error as before. Any help would be appreciated, thanks.
UPDATE: Im a beginner at this, but if im correct, change_categories should be in categories controller, because it's searching for that method in product controller,
POST "/admin/products/change_categories"
So I added categories resource to Active Admin and added that method to categories controller, but is there now a way to use that controller? Something like:
f.input :department, :input_html => { :onchange => remote_request(controller => "categories", :post, :change_categories, {:department_id=>"$('#department_id').val()"}, :category_id) }
Upvotes: 0
Views: 1911
Reputation: 65
So after countless hours I have managed to get this to work. I think problem was in old versions or difference between models from where I took the code. So in Active Admin instead of member_action I used collection_action that makes more sense in routing, because member_action creates route /admin/products/:id/change_categories(.:format) that interferes with show action and uses it instead of change_categories action.
So heres collection_action:
collection_action :change_categories, :method => :get do
@categories = Category.where("department_id = ?", Department.find(params[:product_department_id]))
render :text => view_context.options_from_collection_for_select(@categories, :id, :name)
end
This generates correct route admin/products/change_categories where I can now pass params[:product_departmemnt_id] which is selected id from select box.
This is where it gets Id from:
f.input :description
f.input :department, prompt: "Select department", :input_html => {
onchange: remote_get("change_categories", 'product_department_id', :product_category_id)
}
f.input :category
Tip for some ajax beginners, make sure to check source code of webpage to see if you are using the right id or class or whatever, this has caused headaches for me.
Next thing you want to add is helper method, there are two ways to do this, either in application_helper.rb or in app/helpers. If you add it to appication_helper.rb you have to include it to config/initializers/active_admin.rb initializer:
ActiveAdmin.setup do |config|
....
some config
....
end
module ActiveAdmin::ViewHelpers
include ApplicationHelper
end
But this then has access to all of its helper methods which I don't think is needed or might/might not slow down it, so I created new folder in app/helpers called "active_admin" and created a file "form_helper.rb" where is this code from Dglgmut/6328501 git
def remote_get(path, member,target_tag_id)
"$.get('#{path}/?#{member}=' + $('##{member}').val(),
function(data) {$('##{target_tag_id}').html(data);}
);"
end
Don't know much about js, but I assume this makes get request to admin/products/change_categories/?product_department_id=1 and the rest happens in helper method. If anyone stumbles upon this, I hope this helps you and has information you need. I also suggest you so called "rubber duck" debugging, no jokes, this helps sometimes.
Upvotes: 0
Reputation: 780
Problem is your member_action method is "get" and you are posting to the controller. Change from "POST" to do a Get request should solve the problem.
Upvotes: 0