【问题标题】:JQuery can't find the Sub Category IDJQuery 找不到子类别 ID
【发布时间】:2016-11-12 03:42:55
【问题描述】:

我按照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]]

我的 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 在它不起作用时得到了保存。有什么想法吗?

【问题讨论】:

  • 能否贴出代码的url,以便直接测试?
  • 想要一个 github 链接?
  • 是的,如果可能的话
  • @GraveyardQueen github.com/jayhaz/jquerryissue ,确保你 rake db:seed 和 rake db:migrate
  • 你能把你在github上托管的编码网站的url发过来吗,它将托管在github.io上

标签: javascript jquery ruby-on-rails


【解决方案1】:

浏览了你的代码,我发现了一些错误。

以下是您应该进行的更改以使您的项目正常运行。

正如你所说,这不是任何 jquery 问题。

错误1:-

你把subcategory模型名取为SubCategory,表是sub_categories,所以外键应该是sub_category_id,但是你取了subcategory_id

因此,要么您必须更改数据库中的列,要么告诉 rails 使用名称。

这里是告诉 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:-

posts/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>

这是工作图像:

【讨论】:

  • 可行,但是现在 jquery 不会过滤掉与我的新帖子中的主要类别无关的子类别
  • 非常感谢,将那个脚本放在其他地方而不是在 html.erb 中不是更好吗?
  • 是的,您可以将该脚本移动到 js 文件并使用此视图调用它。如果你觉得它有用,请投票给我的答案:)
  • 已将其移至我的部分表单中,以便我在编辑时也可以使用它,谢谢。
猜你喜欢
  • 2022-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多