Ruby on rails Rails中的M:N关系错误

Ruby on rails Rails中的M:N关系错误,ruby-on-rails,activerecord,nested-forms,Ruby On Rails,Activerecord,Nested Forms,我在试图通过m:n建立Post和Hashtag之间的关系时出错了 一切都很好,但当你试图保存这样的日志打印时 Started POST "/posts" for 127.0.0.1 at 2018-03-05 21:43:00 +0900 Processing by PostsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"DxRb+4PAfOsDTCEMx+BcBNKK/LWA

我在试图通过m:n建立Post和Hashtag之间的关系时出错了

一切都很好,但当你试图保存这样的日志打印时

Started POST "/posts" for 127.0.0.1 at 2018-03-05 21:43:00 +0900
Processing by PostsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"DxRb+4PAfOsDTCEMx+BcBNKK/LWAH5NxwVh6n7OChtwBL/svUQVEBj7mrWdCYB2BIUHO/Gql9UC6mKLMMoPqEg==", "post"=>{"title"=>"please", "content"=>"please!!!!!", "hashtags_attributes"=>{"0"=>{"title"=>"hash1"}, "1"=>{"title"=>"hash2"}, "2"=>{"title"=>"hash3"}}}, "commit"=>"Create Post"}
Unpermitted parameter: hashtags_attributes
Unpermitted parameters: title, content
  Hashtag Load (0.2ms)  SELECT  "hashtags".* FROM "hashtags" WHERE "hashtags"."title" = ? LIMIT ?  [["title", "hash1"], ["LIMIT", 1]]
Unpermitted parameters: title, content
  Hashtag Load (0.1ms)  SELECT  "hashtags".* FROM "hashtags" WHERE "hashtags"."title" = ? LIMIT ?  [["title", "hash2"], ["LIMIT", 1]]
Unpermitted parameters: title, content
  Hashtag Load (0.1ms)  SELECT  "hashtags".* FROM "hashtags" WHERE "hashtags"."title" = ? LIMIT ?  [["title", "hash3"], ["LIMIT", 1]]
   (0.0ms)  begin transaction
  SQL (0.3ms)  INSERT INTO "posts" ("title", "content", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["title", "please"], ["content", "please!!!!!"], ["created_at", "2018-03-05 12:43:00.937624"], ["updated_at", "2018-03-05 12:43:00.937624"]]
  SQL (0.1ms)  INSERT INTO "hashtags_posts" ("hashtag_id", "post_id") VALUES (?, ?)  [["hashtag_id", 1], ["post_id", 2]]
   (0.3ms)  rollback transaction
Completed 500 Internal Server Error in 12ms (ActiveRecord: 1.4ms)



ActiveModel::MissingAttributeError (can't write unknown attribute `id`):

app/controllers/posts_controller.rb:36:in `block in create'
app/controllers/posts_controller.rb:35:in `create'
  Rendering /usr/local/var/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
  Rendering /usr/local/var/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered /usr/local/var/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (7.4ms)
  Rendering /usr/local/var/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered /usr/local/var/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.0ms)
  Rendering /usr/local/var/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered /usr/local/var/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.1ms)
  Rendered /usr/local/var/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (124.9ms)
我不知道为什么标题内容和hashtags\u属性是不允许的参数。我正确地将它们设置在白名单中

这是我的密码

posts\u controller.rb

  def create
    @post = Post.new(post_params)
    3.times do |x|
      tag = hashtag_params[:hashtags_attributes]["#{x}"]["title"]
      a = Hashtag.find_or_create_by(title: tag)
      @post.hashtags << a
    end

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render :show, status: :created, location: @post }
      else
        format.html { render :new }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  def hashtag_params
      params.require(:post).permit(hashtags_attributes: [:title])
  end
这是我的帖子

class Post < ApplicationRecord
    has_and_belongs_to_many :hashtags
    accepts_nested_attributes_for :hashtags
end
这是我的hashtag.rb

class Hashtag < ApplicationRecord
  has_and_belongs_to_many :posts
end
最后,my_form.html.erb

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

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

  <div class="field">
    <%= f.label :title %>
    <%= f.text_field :title %>
  </div>

  <div class="field">
    <%= f.label :content %>
    <%= f.text_area :content %>
  </div>

  <div class="field">
    <%= f.fields_for :hashtags do |h|%>
      <%=h.label :title, "해시태그"%>
      <%=h.text_field :title%>
    <% end %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

您只需正确指定post_参数:

def create
  @post = Post.new(post_params)

  respond_to do |format|
    if @post.save
      format.html { redirect_to @post, notice: 'Post was successfully created.' }
      format.json { render :show, status: :created, location: @post }
    else
      format.html { render :new }
      format.json { render json: @post.errors, status: :unprocessable_entity }
    end
  end
end

def post_params
  params.require(:post).permit(:title, :content, hashtags_attributes: [:id, :title])
end
你可以读更多

您需要更改迁移

class CreateJoinTableHashtagsPosts < ActiveRecord::Migration[5.0]
  def change
    create_join_table :hashtags, :posts do |t|
      t.index :hashtag_id
      t.index :post_id
    end
  end
end
之后,您需要运行rake db:rollback&&rake db:migrate。
或者,您可以使用rake db:drop&&rake db:create&&rake db:migrate从头开始重新创建数据库,在这种情况下,您将丢失所有现有数据

,您的新操作是什么?fields_for应该自动为id生成隐藏字段,但我在参数中看不到它new@post=post.new 3.times{@post.hashtags.new}end这是我的新操作谢谢你的回答但这不起作用这是我的全部代码你有相同的错误吗不能用我的答案中的代码编写未知属性id?试着在表单中添加更多为什么要再次删除并创建de db?不要建议在没有警告的情况下运行db:drop,因为所有数据都将被销毁,这可能是显而易见的,但并非对所有人都是如此。