2016-11-12 1 views
0

を見つけることができませんでした。this question's answer by emmanuelの指示に従ったので、フォームはカテゴリーIDを見つけてサブミットしますが、カテゴリーに関連付けられたサブカテゴリー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が選択ボックスでフォームに取り込まれる部分:それはときに私の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>

混乱機能していませんでした。何か案は?

+0

あなたは、コードのURLを投稿することができますか? – 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 

行はコメントし確認してください。

私が解決したいくつかのエラーが表示されました。

ポスト/ show.html - :誤差2

。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が間違っています。 Error3

: -

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