2015-07-01 63 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