我的要求 基本上我将书籍添加到数据库中,我想将作者存储在一个单独的表中。所以我有一个叫做作者的表,由表格书引用。
我想创建一个用于添加图书的rails表单,我希望它只是Author,Title,Publisher等的简单表单,如果它发现作者已经在authors表中那么它应该只是引用该记录,如果它不在authors表中,那么它应该添加一条新记录并引用它。
存储我在日志文件中获取的值 未经许可的参数:作者
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 %>
~
嗨,先生,我是新手,我知道它看起来很简单,但我找不到解决方案
〜
〜
答案 0 :(得分:1)
这是因为books_params
根据您的参数author_attributes
时author
为def books_params
params.require(:book).permit(:book_name, :price, :author => [:author_name, :title, :publisher])
end
。
这应该有效:
;
; odbc.ini
;
[ODBC Data Sources]
NZSQL = NetezzaSQL
[NZSQL]
Driver = /usr/local/nz/lib64/libnzodbc.so
Description = NetezzaSQL ODBC
Servername = 192.168.118.128
Port = 5480
Database = TESTDB
Username = admin
Password = somepassword
ReadOnly = false
ShowSystemTables = false
LegacySQLTables = false
LoginTimeout = 0
QueryTimeout = 0
DateFormat = 1
NumericAsChar = false
SQLBitOneZero = false
StripCRLF = false
ClientUserID=someuser
ClientWorkStnName=someworkstation
ClientApplName=someapplication
ClientAcctString=someacctstring
[ODBC]
IANAAppCodePage=4
InstallDir=/opt/odbc32v51
Trace=0
TraceDll=/opt/odbc32v51/lib/odbctrac.so
TraceFile=odbctrace.out
UseCursorLib=0
答案 1 :(得分:0)
在视图文件中将authore
更改为author_attributes
,就像这样。
<%= f.fields_for :author_attributes do |b| %>
<p>
<%= b.label :author_name%><br />
<%= b.text_field :author_name %>
</p>
也分别在控制器中更改。我想,这已经足够了。
def create
@book = Book.new(books_params)
@book.save
redirect_to root_url
end
答案 2 :(得分:0)
我建议你debug
你的代码
def books_params
byebug
params.require(:book).permit(:book_name, :price, :author => [:author_name, :title, :publisher])
end
1:检查请求的参数
2:请核对params.require(:book)
请求的参数类似于
Parameters: {"utf8"=>"✓", "authenticity_token"=>"8sxeOVMVJucl5nN+kMpSaT1cB1yDPnk5gfKElWPiT5k=", "book"=>{"book_name"=>"life ", "price"=>"20", "author_"=>{"author_name"=>"swamy", "title"=>"LIFE", "publisher"=>"cta"}}, "commit"=>"Submit"}
check with this link NestedAttributes
你的控制器就是这个
class BooksController < ApplicationController
def new
end
def create
@author = Author.create(params[:book])
redirect_to root_url
end
private
def books_params
params.require(:book).permit(:book_name, :price,:author_attributes => [:author_name, :title, :publisher])
end
end
答案 3 :(得分:0)
您的代码充满了错误。跟随我的变化。
#book.rb
class Book < ActiveRecord::Base
belongs_to :author
accepts_nested_attributes_for :author
end
#books_controller.rb
def new
@book = Book.new
@book.build_author
end
def create
@book = Book.new(books_params)
if @book.save
redirect_to root_url
end
end
最后以 表单 将<%= form_for Book.new do |f| %>
更改为<%= form_for @book do |f| %>