2017-07-05 9 views
2

ネストされた属性を持つ2つのモデルがあります。私のフォームはブック:タイトルを更新できますが、ネストされた属性は更新できません。私が提出した後、私は、私はinverse_ofoptional: trueautosave: trueでモデルを試しただけ(0.2ms)begintransaction (0.2ms)commit transaction.Rails 5ネストされた属性は更新を保存しません

私はそれを解決するために一日を過ごしたPARAMATERSは、端末に起こっているが、何のロールバックがありません参照してください。しかし、まだ更新を保存できません。また、許可されていないパラメータエラーもありません。別の問題があります。

モデル:

has_many :pages 
accepts_nested_attributes_for :pages 

コントローラー:

def update 
    if @book.update_attributes(book_params) 
    redirect_to @book 
    else 
    render 'edit' 
    end 
end 

def book_params 
    params.require(:book).permit(:title, pages_attributes: [:id, :state]) 
end 

マイ形式:

<%= form_for(@book) do |f| %> 
    <%= f.label :title %> 
    <%= f.text_field :title, :autofocus => true, class: 'form-control' %> 

    <%= f.fields_for :pages do |builder| %> 
    <%= builder.select(:state, options_for_select({ "close" => 0, "open" => 1, })) %> 
    <% end %> 
    <%= f.submit 'Submit', %> 
<% end %> 

例コンソール結果:

book = Book.first 
book.update(title:"test", pages_attributes: [id: 124142 , book_id: 1, state: 1 ]) 
    (0.2ms) begin transaction 
    (0.2ms) commit transaction 

EDIT

Serverログ:

Started PATCH "/book/firstbook" for 127.0.0.1 at 2017-07-05 21:57:37 +0300 
Processing by booksController#update as HTML 
    Parameters: { 
    "utf8"=>"✓", 
    "authenticity_token"=>"pElKKQq+M/5GuEG6nJ6Ac1vkEHyIknA2vPiDC9ND+50tq34nDtCRRX9k6TxaMZCInufp68m6BnO8jt4BsJ1bFg==", 
    "book"=>{ 
     "title"=>"firstbook", 
     "pages_attributes"=>{ 
     "0"=>{"state"=>"0", "id"=>"1"}, 
     "1"=>{"state"=>"0", "id"=>"2"}, 
     "2"=>{"state"=>"0", "id"=>"3"}, 
     "3"=>{"state"=>"0", "id"=>"4"}, 
     "4"=>{"state"=>"0", "id"=>"5"}, 
     "5"=>{"state"=>"0", "id"=>"6"}, 
     "6"=>{"state"=>"0", "id"=>"7"}, 
     "7"=>{"state"=>"0", "id"=>"8"}, 
     "8"=>{"state"=>"0", "id"=>"9"}, 
     "9"=>{"state"=>"0", "id"=>"10"}, 
     } 
    }, 
    "commit"=>"submit", "id"=>"firstbook" 
    } 
    book Load (0.2ms) SELECT "books".* FROM "books" WHERE "books"."slug" = ? ORDER BY "books"."id" ASC LIMIT ? [["slug", "firstbook"], ["LIMIT", 1]] 
    (0.1ms) begin transaction 
    (0.1ms) commit transaction 
Redirected to http://localhost:3000/book/firstbook 
Completed 302 Found in 48ms (ActiveRecord: 0.5ms) 
+1

フォームを送信するときにサーバーログを表示できますか? –

+0

私は編集した質問をしました。なぜロールバックがないのかわかりません。私はレールを使用しています5.02 – cotut

+0

それは正しく見えます。あなたのページモデルには何らかのバリデーションがありますか?両方のモデルの完全なコードを投稿できますか? –

答えて

2

あなたはf.fields_for :pages, @book.pages.build do |builder|を試してみてもらえますか? 私はこれがうまくいくことを100%確信しているわけではありませんが、私はしばらくの間フィールドと同じ問題を抱えていました。

+0

ありがとう、それは仕事をしなかったが、これは<%= f.fields_for:pages、@ book.pages.each do | builder | %> – cotut

関連する問題