2016-11-12 54 views
0

我按照this question's answer by emmanuel中的说明操作,表单现在找到类别ID并提交它,但没有找到与类别关联的子类别标识并且不保存它。 。采取JQuery无法找到子类别ID

的PARAMS其可通过将本应注意, 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> 
 
\t <%= f.label :category_id%> 
 
\t <%= f.collection_select(:category_id, Category.all, :id, :name,  
 
\t    { prompt: 'Select a category' }, { id: 'category-select' }) %> 
 
    </p> 
 
    <p> 
 
\t <%= f.label :subcategory_id%> 
 
\t <%= 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得到保存,当它没有工作。有任何想法吗?

+0

你能后的网址代码,以便它可以直接测试? – GraveyardQueen

+0

想要一个github链接? – Jack

+0

是的,如果这是可能的 – GraveyardQueen

回答

1

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

以下是您为使项目正常工作所做的更改。

正如你所说,它不是任何jQuery的问题。

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: <%[email protected]%></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

+0

有问题但是现在jquery不会过滤掉与我的新帖子 – Jack

+0

中的主类别不相关的子类别,让我也解决你的问题。 – Sravan

+0

非常感谢,是不是更好,但脚本在别的地方,而不是在html.erb? – Jack