在同一个表

时间:2017-02-21 01:38:18

标签: javascript jquery ruby-on-rails coffeescript

我想在添加新产品时显示所选类别的子类别。

现在我可以选择主要类别,但子类别无法正确显示。 Chrome控制台出错:

  

获取http://localhost:3000/categories/1/get_subcategories 404(未找到)

谢谢

型号: app / models / category.rb

belongs_to :parent_category, :class_name => :Category, :foreign_key => "parent_category_id"

has_many :children_categories, -> { where "is_main_category = false"}, :class_name => :Category, :foreign_key => "parent_category_id"

控制器: app / controllers / admin / products_controller.rb

  def get_subcategories
    respond_to do |format|
      format.js { @subcategories = Category.where(:parent_category_id => params[:id] ) }
    end
  end

咖啡: app / assets / javascripts / admin / products.coffee

jQuery ->
  $('#main_categories #product_category_id').change ->
  selected_id = $('#main_categories #product_category_id option:selected').val()

  $.get "/categories/#{selected_id}/get_subcategories"

js.erb: app / views / admin / products / get_subcategories.js.erb

$("#sub_categories #product_category_id").html('<%=j options_from_collection_for_select(@subcategories, "id", "name") %>');

观点: app / views / admin / products / _form.html.erb

<%= simple_form_for [:admin, @product] do |f| %> 

  <div class="group" id="main_categories"><%= f.input :category_id,
                                :collection => @main_categories,
                                label: "Main Category",
                                prompt: "Select main Category" %></div>
  <div class="group" id="sub_categories"><%= f.input :category_id,
                                :collection => @subcategories,
                                label: "Sub Category",
                                prompt: "Select sub Category",
                                class: "sub_categories" %></div>

路线: config / routes.rb

namespace admin do
  resources :categories do
    member do
      get :get_subcategories, defaults: { format: "js" }
    end
  end
end

更新

路线走到正确的地方后,我收到了以下错误

  • 来自Chrome
      

    ActionController :: InvalidCrossOriginRequest在Admin :: CategoriesController#get_subcategories

  

安全警告:另一个站点上的嵌入式标记请求受保护的JavaScript。如果您知道自己在做什么,请继续并禁用此操作的伪造保护,以允许跨源JavaScript嵌入。

  • 来自Rails控制台:
  

找不到Admin :: CategoriesController#get_subcategories的模板,渲染头:no_content

1 个答案:

答案 0 :(得分:1)

我明白了。

get_subcategories.js.erb 文件位置错误。

js.erb:app / views / admin / products / get_subcategories.js.erb

应该在 app / views / admin / categories / get_subcategories.js.erb 中。

  

以下是完整的代码:

型号: app / models / category.rb

belongs_to :parent_category, :class_name => :Category, :foreign_key => "parent_category_id"

has_many :children_categories, -> { where "is_main_category = false"}, :class_name => :Category, :foreign_key => "parent_category_id"

控制器: app / controllers / admin / categories_controller.rb

  protect_from_forgery except: :get_subcategories

  def get_subcategories
    @subcategories = Category.where(:parent_category_id => params[:id])
    respond_to do |format|
      format.js
    end
  end

咖啡: app / assets / javascripts / admin / products.coffee

jQuery ->
  $('#main_categories #product_category_id').change ->
  selected_id = $('#main_categories #product_category_id option:selected').val()

  $.get "/admin/categories/#{selected_id}/get_subcategories"

js.erb: app / views / admin / categories / get_subcategories.js.erb

$("#sub_categories #product_category_id").html('<%=j options_from_collection_for_select(@subcategories, "id", "name") %>');

观点: app / views / admin / products / _form.html.erb

<%= simple_form_for [:admin, @product] do |f| %> 

  <div class="group" id="main_categories"><%= f.input :category_id,
                                :collection => @main_categories,
                                label: "Main Category",
                                prompt: "Select main Category",
                                :remote => true %></div>
  <div class="group" id="sub_categories"><%= f.input :category_id,
                                :collection => @subcategories,
                                label: "Sub Category",
                                prompt: "Select sub Category",
                                class: "sub_categories" %></div>

路线: config / routes.rb

namespace admin do
  resources :categories do
    member do
      get :get_subcategories, defaults: { format: "js" }
    end
  end
end