我想在添加新产品时显示所选类别的子类别。
现在我可以选择主要类别,但子类别无法正确显示。 Chrome控制台出错:
获取http://localhost:3000/categories/1/get_subcategories 404(未找到)
谢谢
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"
def get_subcategories
respond_to do |format|
format.js { @subcategories = Category.where(:parent_category_id => params[:id] ) }
end
end
jQuery ->
$('#main_categories #product_category_id').change ->
selected_id = $('#main_categories #product_category_id option:selected').val()
$.get "/categories/#{selected_id}/get_subcategories"
$("#sub_categories #product_category_id").html('<%=j options_from_collection_for_select(@subcategories, "id", "name") %>');
<%= 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>
namespace admin do
resources :categories do
member do
get :get_subcategories, defaults: { format: "js" }
end
end
end
更新:
路线走到正确的地方后,我收到了以下错误
ActionController :: InvalidCrossOriginRequest在Admin :: CategoriesController#get_subcategories
安全警告:另一个站点上的嵌入式标记请求受保护的JavaScript。如果您知道自己在做什么,请继续并禁用此操作的伪造保护,以允许跨源JavaScript嵌入。
找不到Admin :: CategoriesController#get_subcategories的模板,渲染头:no_content
答案 0 :(得分:1)
我明白了。
get_subcategories.js.erb 文件位置错误。
js.erb:app / views / admin / products / get_subcategories.js.erb
应该在 app / views / admin / categories / get_subcategories.js.erb 中。
以下是完整的代码:
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"
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
jQuery ->
$('#main_categories #product_category_id').change ->
selected_id = $('#main_categories #product_category_id option:selected').val()
$.get "/admin/categories/#{selected_id}/get_subcategories"
$("#sub_categories #product_category_id").html('<%=j options_from_collection_for_select(@subcategories, "id", "name") %>');
<%= 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>
namespace admin do
resources :categories do
member do
get :get_subcategories, defaults: { format: "js" }
end
end
end