2017-11-19 7 views
0

Google Books APIを使用するRails 5.1アプリがあり、フォームにネストされたフィールドをあらかじめ入力する必要があります。 Bookを作成するには2つの方法があります。Rails 5.1でネストされたフォームを事前に設定しているときの不正なHTML

まず、通常の/books/new形式のaccepts_nested_attributes_for :authorshas_many: throughに関連付けます。ここで私はcocoonの宝石を使用しており、すべてがうまくいきます。

第2に、ユーザーはISBN番号を使用してGoogle Books APIを検索してBookを作成できます。このデータは、BooksControllercreateアクションに送信される前にフォームにあらかじめ入力されます。私は、このデータをネストされたAuthorのデータを正しく提出する能力とは別に、これを素晴らしいものにすることができました。

私は現在、フォームにフィールドを入力していますが、データを送信するときは、著者アレイの最後のアイテム(複数の著者がいる書籍の場合)のみが保存されます。

これは、フォームhtmlが次のように両方のフィールドで同じnameidを持っているためと考えています。このフォームを作成して両方の著者に提出するにはどうすればよいですか?

<input value="John J. Ratey" type="text" name="book[authors_attributes][0][name]" id="book_authors_attributes_0_name"> 
<input value="Richard Manning" type="text" name="book[authors_attributes][0][name]" id="book_authors_attributes_0_name"> 

books_controller.rb

class BooksController < ApplicationController 

before_action :authenticate_user! 
before_action :set_book, except: [:index, :new, :create, :new_book, :submit_book] 

def create 
    @book = current_user.books.create(book_params) 
    @book.authors.each {|author| author.user_id = current_user.id} 

    if @book.save 
     redirect_to book_path(@book) 
    else 
     render :new 
    end 
end 

def new_book 
end 

def submit_book 
    @book = Book.new 
    @book.authors.new 
    @response = GoogleBooks.new(params[:q], @book) 
end 

private 

    def set_book 
     @book = Book.find(params[:id]) 
    end 

    def book_params 
     params.require(:book).permit(:title, :subtitle, :description, author_ids:[], authors_attributes: [:id, :name, :_destroy]) 
    end 

end 

book.rb

class Book < ApplicationRecord 
    has_many :book_authors 
    has_many :authors, through: :book_authors 

    accepts_nested_attributes_for :authors, allow_destroy: true 

    validates :title, presence: true 
    validates_associated :authors 
end 

google_books.rb

class GoogleBooks 
    include HTTParty 
    base_uri 'https://www.googleapis.com/books/v1' 

    def initialize(isbn, book) 
    @query = self.class.get("/volumes?q=isbn:#{isbn}") 
    @book = book 
    end 

    def title 
    @query['items'].first['volumeInfo']['title'] 
    end 

    def subtitle 
    @query['items'].first['volumeInfo']['subtitle'] 
    end 

    def description 
    @query['items'].first['volumeInfo']['description'] 
    end 

    def authors 
    @query['items'].first['volumeInfo']['authors'] 
    #=> ['John J. Ratey', 'Richard Manning'] 
    end 

end 

submit_book.html.erb

<%= form_for @book do |f| %> 
    <%= f.text_field :title, value: @response.title %> 
    <%= f.text_field :subtitle, value: @response.subtitle %> 
    <%= f.text_field :description, value: @response.description %> 

    <%= f.fields_for :authors, @book.authors.build do |authors_fields| %> 
    <% @response.authors.each do |author| %> 
     <%= authors_fields.text_field :name, value: author %> 
    <% end %> 
    <% end %> 

<%= f.submit %> 
+0

を使用すると、IDに[]追加することはできますか? – Psi

+0

@Psiあなたは何を意味するのか正確に説明できますか? – BillyBib

答えて

0

それを働きました。

は、次の正しいHTML生成し、この

<% @response.authors.each do |author| %> 
    <%= f.fields_for :authors, @book.authors.build do |authors_fields| %> 
    <%= authors_fields.text_field :name, value: author %> 
    <% end %> 
<% end %> 

<%= f.fields_for :authors, @book.authors.build do |authors_fields| %> 
    <% @response.authors.each do |author| %> 
    <%= authors_fields.text_field :name, value: author %> 
    <% end %> 
<% end %> 

を変更する場合ました:

<input value="John J. Ratey" type="text" name="book[authors_attributes][0][name]" id="book_authors_attributes_0_name"> 
<input value="Richard Manning" type="text" name="book[authors_attributes][1][name]" id="book_authors_attributes_1_name"> 
関連する問題