문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top