2016-10-12 2 views
0

나는 레일스의 멍청한 놈이다. 3-4 일 동안이 작업을 해왔다. 내가 대다을 만들었습니다 : 나는 새로운 객체를 제출하면 협회를 통해,하지만,이 오류가 발생합니다 :레일 다 대다 : 연결을 통해

#<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