【发布时间】:2016-02-27 13:48:42
【问题描述】:
我创建了一个表单,它允许用户通过单击文本或 URL 按钮来动态添加嵌套表单字段。可以添加 Text 或 Url 字段的任何排列(也可以删除它们)。
提交表单时,内容会显示在视图模板中。但是,当我在 /posts/id/edit 编辑帖子时,帖子内容不会出现在编辑模板中 - 这是一个空白页面。
SQL日志http://i.stack.imgur.com/B2ueq.png
后模型
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.includes(:things).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 %>
edit.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
删除字段
jQuery ->
$('form').on 'click', '.remove_fields', (event) ->
$(this).prev('input[type=hidden]').val('1')
$(this).closest('div').remove()
event.preventDefault()
添加字段(新模板)
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}" />
<input type="hidden" name="post[thing_attributes][#{current_index}][_destroy]" id="post_things_attributes_#{current_index}__destroy" value="#current_index" />
<a class="remove_fields" href="#">x</a>
</div>
"""
$("#new_post input[type='submit']").before(html)
current_index += 1
$ ->
$('#addtext').on('click', addText)
current_index = 0
addUrl = ->
html = """
<div>
<input placeholder="http://www..." type="url" name="post[things_attributes][#{current_index}][url]" id="post_things_attributes_#{current_index}_url">
<input type="hidden" name="post[things_attributes][#{current_index}][order]" id="post_things_attributes_#{current_index}_order" value="#{current_index}" />
<input type="hidden" name="post[thing_attributes][#{current_index}][_destroy]" id="post_things_attributes_#{current_index}__destroy" value="#current_index" />
<a class="remove_fields" href="#">x</a>
</div>
"""
$("#new_post input[type='submit']").before(html)
current_index += 1
$ ->
$('#addurl').on('click', addUrl)
添加字段(编辑模板)
current_index = 0
editText = ->
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}" />
<input type="hidden" name="post[thing_attributes][#{current_index}][_destroy]" id="post_things_attributes_#{current_index}__destroy" value="#current_index" />
<a class="remove_fields" href="#">x</a>
</div>
"""
$(".edit_post input[type='submit']").before(html)
current_index += 1
$ ->
$('#edittext').on('click', editText)
editUrl = ->
html = """
<div>
<input placeholder="http://www..." type="url" name="post[things_attributes][#{current_index}][url]" id="post_things_attributes_#{current_index}_url">
<input type="hidden" name="post[things_attributes][#{current_index}][order]" id="post_things_attributes_#{current_index}_order" value="#{current_index}" />
<input type="hidden" name="post[thing_attributes][#{current_index}][_destroy]" id="post_things_attributes_#{current_index}__destroy" value="#current_index" />
<a class="remove_fields" href="#">x</a>
</div>
"""
$(".edit_post input[type='submit']").before(html)
current_index += 1
$ ->
$('#editurl').on('click', editUrl)
【问题讨论】:
-
我没有看到任何 Java 代码
-
我的意思不是按 javascript - 抱歉!
-
请不要自己编写这个 javascript - 你不会玩得开心(有很多事情需要考虑)。有一个很棒的宝石就是为此而写的:cocoon github.com/nathanvda/cocoon
-
@BroiSatse - 感谢您的评论!我会使用茧宝石
-
我的nested_form_fields gem 也适用于此:github.com/ncri/nested_form_fields
标签: javascript jquery ruby-on-rails forms ruby-on-rails-4