Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails中通过表的多对多关系的嵌套表单不保存属性_Ruby On Rails_Ruby_Nested Forms - Fatal编程技术网

Ruby on rails rails中通过表的多对多关系的嵌套表单不保存属性

Ruby on rails rails中通过表的多对多关系的嵌套表单不保存属性,ruby-on-rails,ruby,nested-forms,Ruby On Rails,Ruby,Nested Forms,我有一个Blog模型和一个标记模型,它们通过表(model:BlogTag)Blog_标记具有多对多关联。我已经在博客中实现了一个嵌套表单来添加标签 我在参数中得到了标签和属性。但是当我保存blog对象时,它确实会将tag对象保存到blog中,但是它确实会通过name=nil保存它 博客模式 下面可以看到,在table tag的insert命令中,名称字段并没有得到值,因此变为nil 服务器日志 (1.2ms)开始 SQL(0.9ms)在“blogs”(“blog”、“title”、“slugu

我有一个Blog模型和一个标记模型,它们通过表(model:BlogTag)Blog_标记具有多对多关联。我已经在博客中实现了一个嵌套表单来添加标签

我在参数中得到了标签和属性。但是当我保存blog对象时,它确实会将tag对象保存到blog中,但是它确实会通过name=nil保存它

博客模式

下面可以看到,在table tag的insert命令中,名称字段并没有得到值,因此变为nil

服务器日志

(1.2ms)开始
SQL(0.9ms)在“blogs”(“blog”、“title”、“slugurl”、“created_u at”、“updated_at”)值($1、$2、$3、$4、$5)中插入返回“id”[[“blog”、“adsfafsfas

\r\n”]、[“title”、“test 11”]、[“slugurl”、“asdff”]、[“created_at”、“2017-07-19 08:31:32.677338”]、[“updated_at”、“2017-07-19 08:31:32.677338”] SQL(0.6ms)插入到“标记”(“创建时间”,“更新时间”)值($1,$2)中,返回“id”[[“创建时间”,“2017-07-19 08:31:32.684302”],[“更新时间”,“2017-07-19 08:31:32.684302”]] SQL(1.1ms)插入“博客标签”(“博客id”、“标签id”、“创建时间”、“更新时间”)值($1,$2,$3,$4)返回“id”[[“博客id”,20],“标签id”,18],“创建时间”,“2017-07-19 08:31:32.706957”],[“更新时间”,“2017-07-19 08:31:32.706957”] (8.3ms)提交 重定向到http://localhost:3000/blogs 在33792ms内完成302次(ActiveRecord:15.8ms)
Params

{“utf8”=>“✓", "真实性令牌“=>”+1ZLYPJkQCVOkOSt6dmy9z12XgOMP+OYu0XOOzKUlv+xldd5fkB2RR/oA7qpn53YE+82FDJVEO2YLHKPIEWFWVW=”,“blog”=>“{”title“=>“编程语言”,“标记属性”=>{“0”=>{“name”=>[“Python”,“C#”,“.NET”}>,“slugurl”=>“pro lang”,“blog”=>“ASDFASDF afaadf afaadf=>,“提交博客”=>
表格(视图)


禁止保存此博客:




我在参数中得到了标签和属性。但是当我保存博客的时候 对象它确实将标记对象保存到它,但它确实按名称保存它= 零

根据
参数
散列,您正在为
名称
发送多个值,因此需要修改
blog_参数
以接受
名称
的多个值

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

使用日志中显示的表单和参数哈希更新您的问题,请立即查看。@RaounaqSharma乐于帮助:)将答案标记为已接受:)
class Tag < ActiveRecord::Base
  has_many :blog_tags, dependent: :destroy
  has_many :blogs, through: :blog_tags
end
class BlogTag < ActiveRecord::Base
  belongs_to :blog
  belongs_to :tag
end
  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)
{"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 %>
def blog_params
  params.require(:blog).permit(:blog,
                               :title,
                               :slugurl,
                               tags_attributes: [name: []]
  )
end