Question

Hi i am kind of rails newbie and i have two models Category and Product

class Category < ActiveRecord::Base
 attr_accessible :name, :parent_id
 has_many :products
 acts_as_nested_set
 validates_presence_of :name
end

class Product < ActiveRecord::Base
 attr_accessible :name, :category_id
 belongs_to :category
 validates_presence_of :name
end

and i want to create dynamically created select item for choosing category in new poduct page with ajax or something else.

For example our categories are

-Cat1
 -Cat1.1
  -Cat1.1.1
 -Cat1.2
-Cat2
 -Cat2.1

etc. i want to first select form came with root categories and create new object until selected category has no child.

I'll glad for any advice. Thanks.

Était-ce utile?

La solution

You can use

url:refresh_category_path, remote: true

property for ajax request in your form and render new form in refresh.js.erb file with

$("#product").append("<%= escape_javascript(render(:partial => @product)) %>");

http://railscasts.com/episodes/136-jquery you can get more info from here.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top