2015-07-01 45 views
1

我的要求 基本上我将书籍添加到数据库,并且我想将作者存储在单独的表格中。所以我有一个表格,这个表格叫做作者,这个表格被桌子所引用。未经许可的参数:作者

我想创建一个用于添加书籍的Rails表单,并且我希望它只是作者,标题,出版商等的简单表单,并且如果它发现作者已经在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

你的代码是完全错误的。按照我的更改。

#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| %>

+0

谢谢先生,但只有书本值存储在数据库中,但作者表没有值,请你解释如何在检查后将数据存储到作者表中你是否已经存在或没有 – user5065277

1

那是因为你books_paramsauthor_attributes何时应该基于author在你的参数上。

这应该工作:

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

感谢您回应,但上面的代码我编辑,但我得到未知属性:符合@book = Book.new(books_params作者错误) – user5065277

0

变化authoreauthor_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 
0

我会建议你只debug代码

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

1:检查要求PARAMS

2:查收params.require(:book)

[R equested参数会像

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 
+0

谢谢,但作者表vales没有存储到作者表中,如何存储作者表值与书表值 – user5065277