我做了一個表單,用戶可以通過單擊文本或url按鈕動態添加嵌套表單字段。提交表單時,內容將顯示在視圖模板中。但是,當我編輯(&希望然後更新)/posts/id/edit
的帖子時,帖子內容不會出現在編輯視圖模板中 - 它是瀏覽器中的空白頁面。Rails嵌套表單 - 編輯動作&edit.html.erb不會顯示錶單或相關文章內容
將非常感謝您的幫助!謝謝!
郵政型號
class Post < ActiveRecord::Base
has_many :things, dependent: :destroy
accepts_nested_attributes_for :things
end
事情型號
class Thing < ActiveRecord::Base
belongs_to :post
end
模式
create_table "posts", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "things", force: :cascade do |t|
t.text "text"
t.string "url"
t.integer "post_id"
end
帖子控制器
class PostsController < ApplicationController
def edit
@post = Post.find(params[:id])
end
def update
end
new.html.erb
<button id='addtext'>text</button>
<button id='addurl'>url</button>
<%= form_for @post, url: posts_path, html: { multipart: true } do |f| %>
<%= f.fields_for :thing do |ff| %>
<% end %>
<%= f.submit %>
<% end %>
posts.coffee
current_index = 0
addText = ->
html = """
<div>
<textarea placeholder="Write something..." name="post[things_attributes][#{current_index}][text]" id="post_things_attributes_#{current_index}_text"></textarea>
<input type="hidden" name="post[things_attributes][#{current_index}] [order]" id="post_things_attributes_#{current_index}_order" value="#{current_index}" />
</div>
"""
$("#new_post input[type='submit']").before(html)
current_index += 1
$ ->
$('#addtext').on('click', addText)
current_index = 0
你應該有一個叫** edit.html.erb **的視圖 – Turk
道歉 - 我知道,它和new.html.erb模板中的代碼相同。 – domburford