2017-07-19 101 views
0

我有一个博客模型和标签模型,它们通过表(模型:BlogTag)blog_tags具有多对多关联。我已经在博客中实施了一个嵌套表单来为它们添加标签。嵌套表格在多对多关系中通过表格不保存属性

我在参数中获得tags_attributes。但是,当我保存博客对象时,它会将标记对象保存到它,但它通过名称= nil保存它。

博客模型

class Blog < ActiveRecord::Base 

    extend FriendlyId 
    friendly_id :slugurl, use: :slugged, slug_column: :slugurl 

    has_many :blog_tags, dependent: :destroy 
    has_many :tags, through: :blog_tags 

    accepts_nested_attributes_for :tags 

    def self.search(search) 
    words = search.to_s.downcase.strip.split.uniq 
    words.map! { |word| "tag ~* '\\y#{word}\\y'" } 
    psql = words.join(" OR ") 
    self.where(psql) 
    end 
end 

标签模型

class Tag < ActiveRecord::Base 
    has_many :blog_tags, dependent: :destroy 
    has_many :blogs, through: :blog_tags 
end 

BlogTag模型

class BlogTag < ActiveRecord::Base 
    belongs_to :blog 
    belongs_to :tag 
end 

blog_controller

def new 
    @blog = Blog.new 
    @blog.tags.build 
    end 

    def create 
    @blog = Blog.new(blog_params) 
    save_tag_for 
    byebug 
    respond_to do |format| 
     if @blog.save 
     format.html { redirect_to blogs_path, notice: 'Blog was successfully created.' } 
     format.json { render :index, status: :created, location: @blog} 
     else 
     format.html { render :new } 
     format.json { render json: @blog.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

def blog_params 
    params.require(:blog).permit(:blog, 
           :title, 
           :slugurl, 
           tags_attributes: [:name] 
) 
end 

下面你可以看到,在表标签插入命令,名称字段没有得到,因此值成为零。

服务器日志

(1.2ms) BEGIN 
    SQL (0.9ms) INSERT INTO "blogs" ("blog", "title", "slugurl", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["blog", "<p>adsfadsfdfas</p>\r\n"], ["title", "test 11"], ["slugurl", "asdfdf"], ["created_at", "2017-07-19 08:31:32.677338"], ["updated_at", "2017-07-19 08:31:32.677338"]] 
    SQL (0.6ms) INSERT INTO "tags" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", "2017-07-19 08:31:32.684302"], ["updated_at", "2017-07-19 08:31:32.684302"]] 
    SQL (1.1ms) INSERT INTO "blog_tags" ("blog_id", "tag_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["blog_id", 20], ["tag_id", 18], ["created_at", "2017-07-19 08:31:32.706957"], ["updated_at", "2017-07-19 08:31:32.706957"]] 
    (8.3ms) COMMIT 
Redirected to http://localhost:3000/blogs 
Completed 302 Found in 33792ms (ActiveRecord: 15.8ms) 

PARAMS

{"utf8"=>"✓", "authenticity_token"=>"+1ZLYPJkQCVOkOSt6dmy9z12XgOMP+OYu0XOOzKUlv+xldd5fkB2RR/oA7qpn53YE+82FDjVeO2ylHkPIEWfVw==", "blog"=>{"title"=>"Programming Languages", "tags_attributes"=>{"0"=>{"name"=>["Python", "C#", ".NET"]}}, "slugurl"=>"pro-lang", "blog"=>"<p>asdfasdfasdf afaadf</p>\r\n"}, "commit"=>"Submit", "controller"=>"blogs", "action"=>"create"} 

表格(图)

<%= form_for(@blog) do |f| %> 
    <% if @blog.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@blog.errors.count, "error") %> prohibited this blog from being saved:</h2> 

     <ul> 
     <% @blog.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

      <div class="row"> 
      <div class="form-group"> 
      <%= f.label :title ,class: 'control-label' %><br> 
      <%= f.text_field :title ,class: 'form-control' %> 

      <div class="help-block with-errors"></div> 
      </div> 
      </div> 

      <div class="row"> 
      <div class="form-group"> 
       <%= f.fields_for :tags do |tag_builder| %> 
        <%= tag_builder.label :name, "Tag", class: 'control-label' %><br> 
        <%= tag_builder.select :name, Tag.all.pluck(:name,:name), {}, {multiple: true, class: "selectize" } %> 
       <%end%> 
       <%#= form.select :category_id, Category.all.pluck(:name,:id), {}, {multiple: true, class: "selectize"} %> 
       <div class="help-block with-errors"></div> 
      </div> 
       </div> 
      <div class="row"> 
      <div class="form-group"> 
      <%= f.label :slugurl ,class: 'control-label' %><br> 
      <%= f.text_field :slugurl ,class: 'form-control' %> 

      <div class="help-block with-errors"></div> 
      </div> 
      </div> 


      <div class="row"> 
      <div class='form-group'> 
      <%= f.label :blog ,class:'control-label'%><br> 
      <%= f.cktext_area :blog, rows: 10,class:'form-control' %> 
      <div class="help-block with-errors"></div> 
      </div> 
      </div> 

       <div class="form-actions mg-t-lg"> 
       <div class="row"> 
        <div class="col-sm-7 col-sm-offset-5"> 
        <div class="text-center-xs"> 
         <input type="submit" name="commit" value="Submit" class="btn btn-contrast" /> 
        </div> 
        </div> 
       </div> 
       </div> 

<% end %> 
+0

更新您的问题与形式和参数散列出现在第日志 – Pavan

+0

完成,请现在审查。 –

回答

0

我在参数中得到tags_attributes。但是,当我保存博客 对象时,它确实将标签对象保存到它,但它确实通过名称= nil保存它。

按照该params哈希,你是为name发送多个值,所以你需要修改blog_params以接受name多个值。

def blog_params 
    params.require(:blog).permit(:blog, 
           :title, 
           :slugurl, 
           tags_attributes: [name: []] 
) 
end 
+1

谢谢你解决了我的问题。 –

+0

@RaounaqSharma很高兴帮助:)标记答案为接受:) – Pavan