2016-10-12 19 views
0

私はRailsに関心があり、これを3-4日間続けてきました。協会を通じて、私は、新しいオブジェクトを送信すると、私はこのエラーを取得する:私は、多対多を作成しましたRails多対多:関連付け

#<ActiveModel::Errors:0x007fbced7cda88 @base=#<BlogPost id: nil, created_at: nil, updated_at: nil, title: "title 13", content: "pajfposjpfej 13", posted_by: "poster 13", comments: nil, blog_pic: nil>, @messages={:"categorizations.blog_post"=>["must exist"], :title=>[], :posted_by=>[], :content=>[], :blog_pic=>[]}, @details={"categorizations.blog_post"=>[{:error=>:blank}]}>

マイフォームの送信、私はそうカテゴリを選択しています提出していますエラーが示すように空であってはなりません。これに関する助けを本当にありがとう。今の日のために士気を失った。

モデル:

class BlogPost < ApplicationRecord 
    has_many :categorizations 
    has_many :categories, :through => :categorizations 
    accepts_nested_attributes_for :categorizations 
    has_many :comments 
    mount_uploader :blog_pic, BlogPicUploader 
end 

class Categorization < ApplicationRecord 
    belongs_to :blog_post 
    belongs_to :category 
end 

class Category < ApplicationRecord 
    has_many :categorizations 
    has_many :blog_posts, :through => :categorizations 
end 

ビュー/ blog_posts/new.html.erb

<%= form_for @blog_post do |b| %> 
      <%= b.label :title %> 
      <%= b.text_field :title %><br> 

      <%= b.fields_for :categorizations do |cat| %> 
       <%= cat.label :category_name, "Category 1" %> 
       <%= cat.collection_select(:category_id, Category.all, :id, :category_name, include_blank: "Select Category") %><br> 
      <% end %> 

      <%= b.submit "Submit", class: "btn btn-primary" %> 
     <% end %> 

コントローラ:

class BlogPostsController < ApplicationController 

    def index 
     @blog_posts = BlogPost.order(id: :desc) 
    end 

    def new 
     @blog_post = BlogPost.new 
     @categorizations = @blog_post.categorizations.build 
     @categories = @blog_post.categories.build 
    end 


... 

    def create 
     @blog_post = BlogPost.new(blog_post_params) 
     respond_to do |format| 
      if @blog_post.save 
      format.html { redirect_to @blog_post, notice: 'Your blog was submitted successfully' } 
      format.json { render :show, status: :created, location: @blog_post } 
      else 
      format.html { render :new } 
      format.json { render json: @blog_post.errors, status: :unprocessable_entity } 
      end 
     end 
     puts @blog_post.errors.inspect 
    end 

private 

    def blog_post_params 
    params.require(:blog_post).permit(:title, :content, :posted_by, :comments, :blog_pic, {categorizations_attributes: [:category_id, :category_name]}) 
    end 

end 

答えて

0

答えはここにコードとは何の関係もありませんでした - 簡単に言えば - レール5に、彼らはきたがデフォルトではbelongs_to関連付けが必要な変更を行いました。これは、Rails 4の場合と同じように、これはオプションです。それは説明する以下のリンクを参照してください:あなたはtrueからfalseに設定/初期化子/ new_framework_defaultsにこの行を変更する必要が

http://blog.bigbinary.com/2016/02/15/rails-5-makes-belong-to-association-required-by-default.html

Rails.application.config.active_record.belongs_to_required_by_default = false 
0

あなたにもcategoriesのためのネストされた属性を宣言する必要があります。

のような:

class BlogPost < ApplicationRecord 
    has_many :categorizations 
    has_many :categories, :through => :categorizations 
    accepts_nested_attributes_for :categorizations 
    accepts_nested_attributes_for :categories 
    has_many :comments 
    mount_uploader :blog_pic, BlogPicUploader 
end 

あなたは説明のためのリンクの下に参照することができ

https://hackhands.com/building-has_many-model-relationship-form-cocoon/

+0

まだ動作しません。他の考え? – dgreen22

関連する問題