gpt4 book ai didi

ruby-on-rails - 不允许的参数 : author

转载 作者:行者123 更新时间:2023-11-29 13:27:29 26 4
gpt4 key购买 nike

我的要求基本上我将书籍添加到数据库中,我想将作者存储在一个单独的表中。所以我有一个名为 authors 的表,由表 books 引用。

我想创建一个用于添加书籍的 Rails 表单,我希望它只是作者、标题、出版商等的简单表单,如果它发现作者已经在作者表中,那么它应该只是引用该记录,如果它不在作者表中,那么它应该添加一个新记录并引用它。

在存储值时我正在日志文件中获取不允许的参数:作者

Processing by BooksController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"8sxeOVMVJucl5nN+kMpSaT1cB1yDPnk5gfKElWPiT5k=", "book"=>{"book_name"=>"life ", "price"=>"20", "author"=>{"author_name"=>"swamy", "title"=>"LIFE", "publisher"=>"cta"}}, "commit"=>"Submit"}

swamy
LIFE
cta
Author Load (1.6ms) SELECT "authors".* FROM "authors" WHERE "authors"."author_name" = 'swamy' AND "authors"."title" = 'LIFE' AND "authors"."publisher" = 'cta' LIMIT 1
(0.6ms) BEGIN
SQL (25.2ms) INSERT INTO "authors" ("author_name", "created_at", "publisher", "title", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["author_name", "swamy"], ["created_at", "2015-07-01 03:53:22.712401"], ["publisher", "cta"], ["title", "LIFE"], ["updated_at", "2015-07-01 03:53:22.712401"]]
(16.8ms) COMMIT
Unpermitted parameters: author
(0.7ms) BEGIN
SQL (1.0ms) INSERT INTO "books" ("Author_id", "book_name", "created_at", "price", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["Author_id", 8], ["book_name", "life "], ["created_at", "2015-07-01 03:53:22.865106"], ["price", 20], ["updated_at", "2015-07-01 03:53:22.865106"]]
(22.4ms) COMMIT

我的代码books_Controller

class BooksController < ApplicationController
def new

end
def create

@a=params[:author_name]
puts @a
b=params[:book][:author][:author_name]
puts b
# @c = @b.authors
# @d = @c.author_name
c=params[:book][:author][:title]
d=params[:book][:author][:publisher]

puts c
puts d
@author = Author.find_or_create_by(author_name: b,title: c, publisher: d)
@book = Book.new(books_params)
@book.Author = @author
@book.save
redirect_to root_url

end
private

def books_params
params.require(:book).permit(:book_name, :price,:author_attributes => [:author_name, :title, :publisher])
end
end

模型

class Author < ActiveRecord::Base

end

class Book < ActiveRecord::Base
belongs_to :Author
accepts_nested_attributes_for :Author
# def author_name=(author_name)
# self.author = Author.find_or_create_by_name author_name
#end
end

查看books.html.erb

<%= form_for Book.new  do |f| %>

<p>
<%= f.label :book_name %><br />
<%= f.text_field :book_name %>
</p>
<p>
<%= f.label :price %><br />
<%= f.text_field :price %>
</p>

<%= f.fields_for :author do |b| %>
<p>
<%= b.label :author_name%><br />
<%= b.text_field :author_name %>
</p>
<p>
<%= b.label :title%><br />
<%= b.text_field :title %>
</p>
<p>
<%= b.label :publisher%><br />
<%= b.text_field :publisher%>
</p>

<% end %>
<p><%= f.submit "Submit" %></p>
<% end %>
~

您好,先生,我是 Rails 的新手,我知道它看起来很简单,但我找不到解决方案

~
~

最佳答案

那是因为你的 books_paramsauthor_attributes 而根据你的参数它应该是 author

这应该有效:

def books_params
params.require(:book).permit(:book_name, :price, :author => [:author_name, :title, :publisher])
end

关于ruby-on-rails - 不允许的参数 : author,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31152384/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com