JQuery找不到子类别ID

时间:2016-11-12 03:42:56

标签: javascript jquery ruby-on-rails

我按照this question's answer by emmanuel中的说明操作,表单现在找到了类别ID并提交了它,但没有找到与类别关联的子类别ID,也没有保存。

可以注意到这些参数, Parameters: {"utf8"=>"✓", "authenticity_token"=>"PTRTGGblf3HoWNXmanKl8TIP7F4j/QKTLN2Wd6oKSQWSXV27qioztUpXgb6YjHEroaWf8dgTzUIgQiRBK2XxWQ==", "post"=>{"title"=>"200k", "description"=>"FMxd123", "category_id"=>"2", "subcategory_id"=>"9"}, "commit"=>"Create Post"}

然后在我的屏幕上显示错误消息(我的错误部分)“子类别必须存在.SQL输出是这样的:

  (0.2ms)  begin transaction
 Category Load (0.1ms)  SELECT  "categories".* FROM "categories" WHERE "categories"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]

   (0.0ms)  rollback transaction
  Rendering posts/new.html.erb within layouts/application
  Rendered shared/_errors.html.erb (0.8ms)
  Category Load (0.1ms)  SELECT "categories".* FROM "categories"
  CACHE (0.0ms)  SELECT "categories".* FROM "categories"

  SubCategory Load (0.1ms)  SELECT "sub_categories".* FROM "sub_categories" WHERE "sub_categories"."category_id" = ?  [["category_id", 1]]
  SubCategory Load (0.1ms)  SELECT "sub_categories".* FROM "sub_categories" WHERE "sub_categories"."category_id" = ?  [["category_id", 2]]
  SubCategory Load (0.1ms)  SELECT "sub_categories".* FROM "sub_categories" WHERE "sub_categories"."category_id" = ?  [["category_id", 3]]

My Posts.coffee:

jQuery ->
  subcat = $('#subcategory-select').html()
  $('#category-select').change ->
    cat = jQuery('#category-select').children('option').filter(':selected').text()
    options = $(subcat).filter("optgroup[label='#{cat}']").html()
    if options
      $('#subcategory-select').html(options)
    else
      $('#subcategory-select').empty()

使用选择框在表单中采用category_id和sub_category_id的部分:

  <p>
	<%= f.label :category_id%>
	<%= f.collection_select(:category_id, Category.all, :id, :name,    
	               { prompt: 'Select a category' }, { id: 'category-select' }) %>
  </p>
  <p>
	<%= f.label :subcategory_id%>
	<%= f.grouped_collection_select :subcategory_id, Category.all, :sub_categories, 
          :name, :id, :name, { include_blank: 'Select a sub category' },
                                               { id: 'subcategory-select' } %>
   </p>

因为它使我的category_id在不工作时被保存而感到困惑。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

通过您的代码,我发现了一些错误。

以下是您应该对项目进行的更改。

正如您所提到的,这不是任何问题。

<强> ERROR1 : -

您已将subcategory模型名称设为SubCategory且表格为sub_categories,因此外键应为sub_category_id,但您已使用subcategory_id

因此,您必须更改数据库中的列,或者告诉rails获取名称。

以下是告诉我们的变化。

<强> post.rb

class Post < ApplicationRecord
  belongs_to :category
  # belongs_to :sub_category
  belongs_to :sub_category, class_name: 'SubCategory', foreign_key: 'subcategory_id'
end

<强> sub_category.rb

class SubCategory < ApplicationRecord
  belongs_to :category
  # has_many :posts, :primary_key => "subcategory_id"
  has_many :posts, class_name: 'Post', primary_key: 'id', foreign_key: 'subcategory_id'
end

检查评论的行。

现在,帖子节目视图也有一些我解决的错误。

<强>误差2 : -

<强>帖/ show.html.erb:

<% content_for :title, @post.title %>
<% navigation_add @post.title, post_path(@post) %>
<h2 align="center">Title: <%= @post.title %></h2>
<div class="well col-xs-8 col-xs-offset-2">
    <h4 class="center description"><strong>Description:</strong></h4>
    <hr>
    <%= simple_format(@post.description) %>
    <hr>
    <p>Post ID: <%=@post.id%></p>
    <hr>
    <div class="post-actions">
        <%= link_to "Edit this post", edit_post_path(@post), class: "btn btn-xs btn-primary" %>
        <%= link_to "Delete this post", post_path(@post), method: :delete,
        data: { confirm: "Are you sure you want to delete the post?"},
        class: "btn btn-xs btn-danger" %>
        <%= link_to "View all posts", posts_path, class: "btn btn-xs btn-success" %>
    </div>
</div>

最后但并非最不重要,您的seeds.rb错误。

<强>误差3 : -

category_1 = Category.where(name:"cat1").first_or_create(name:"cat1")
category_2 = Category.where(name:"cat2").first_or_create(name:"cat2")
#SUB
# 1
SubCategory.where(name: 'g', category_id: category_1.id).first_or_create
SubCategory.where(name: 'er', category_id: category_1.id).first_or_create
#L2
SubCategory.where(name: 'tu', category_id: category_2.id).first_or_create
SubCategory.where(name: 'dual', category_id: category_2.id).first_or_create

将此脚本添加到posts/new.html以使您的下拉列表正常工作。

<script type="text/javascript">
$(document).ready(function() {
  var subcat;
  subcat = $('#subcategory-select').html();
  return $('#category-select').change(function() {
    var cat, options;
    cat = jQuery('#category-select').children('option').filter(':selected').text();
    options = $(subcat).filter("optgroup[label='" + cat + "']").html();
    if (options) {
      return $('#subcategory-select').html(options);
    } else {
      return $('#subcategory-select').empty();
    }
  });
});
</script>

以下是工作图片:

enter image description here