【发布时间】:2018-03-05 12:53:13
【问题描述】:
我在尝试通过 m 建立 Post 和 Hashtag 之间的关系时遇到了一些错误:n
一切都很好,但是当尝试保存这样的日志打印时
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_attributes 是不允许的参数。我将它们正确设置在白名单中
这是我的代码
posts_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
这是我的帖子.rb
class Post < ApplicationRecord
has_and_belongs_to_many :hashtags
accepts_nested_attributes_for :hashtags
end
这是我的标签.rb
class Hashtag < ApplicationRecord
has_and_belongs_to_many :posts
end
最后,我的 _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 %>
【问题讨论】:
-
您的
new操作中有什么内容?fields_for应该会自动为id生成隐藏字段,但是在params中看不到 -
def new @post = Post.new 3.times {@post.hashtags.new} 结束这是我的新动作
-
感谢您的回答,但没有奏效
-
我的回答中的代码是否与
can't write unknown attribute id相同?尝试在他的表单中添加许多<%=h.hidden_field :id%>
标签: ruby-on-rails activerecord nested-forms