2016-11-12 53 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