Javascript Rails-嵌套形式赢得';不显示在编辑模板中
我创建了一个表单,允许用户通过单击文本或Url按钮动态添加嵌套表单字段。可以添加文本或Url字段的任何排列(也可以删除) 示例- 提交表单时,内容将显示在视图模板中。但是,当我在/posts/id/edit编辑一篇文章时,文章内容不会出现在编辑模板中-它是一个空白页面 SQL日志 后期模型Javascript Rails-嵌套形式赢得';不显示在编辑模板中,javascript,jquery,ruby-on-rails,forms,ruby-on-rails-4,Javascript,Jquery,Ruby On Rails,Forms,Ruby On Rails 4,我创建了一个表单,允许用户通过单击文本或Url按钮动态添加嵌套表单字段。可以添加文本或Url字段的任何排列(也可以删除) 示例- 提交表单时,内容将显示在视图模板中。但是,当我在/posts/id/edit编辑一篇文章时,文章内容不会出现在编辑模板中-它是一个空白页面 SQL日志 后期模型 class Post < ActiveRecord::Base has_many :things, dependent: :destroy accepts_nested_attributes_for
class Post < ActiveRecord::Base
has_many :things, dependent: :destroy
accepts_nested_attributes_for :things
end
发布控制器
class Thing < ActiveRecord::Base
belongs_to :post
end
class PostsController < ApplicationController
def edit
@post = Post.includes(:things).find(params[:id])
end
def update
end
添加字段(新模板)
当前索引=0
addText=->
html=”“”
"""
$(“#新建#u发布输入[type='submit']”)。之前(html)
当前_指数+=1
$ ->
$('#addtext')。在('click',addtext')上
当前指数=0
addUrl=->
html=”“”
"""
$(“#新建#u发布输入[type='submit']”)。之前(html)
当前_指数+=1
$ ->
$('#addurl')。在('click',addurl')上
添加字段(编辑模板)
当前索引=0
编辑文本=->
html=”“”
"""
$(“.edit_post input[type='submit']”)。在(html)之前
当前_指数+=1
$ ->
$('edittext')。在('click',edittext')上
editUrl=->
html=”“”
"""
$(“.edit_post input[type='submit']”)。在(html)之前
当前_指数+=1
$ ->
$('editurl')。在('click',editurl')上
需要检查的几件事:
让Rails处理您的路由
在new.html.erb
和edit.html.erb
中,从form_中删除url
键,让Rails找出正确的路径:
<%= form_for @post, html: { multipart: true } do |f| %>
<%= f.fields_for :thing do |ff| %>
<% end %>
<%= f.submit %>
<% end %>
检查日志
事实上,你什么也没看到,这表明你犯了错误。日志中还有什么其他内容吗
$ tail -f log/development.log
还要确保检查是否有任何javascript错误(例如,在Chrome中,打开inspector工具并查找红十字。如果有,请单击它打开控制台并列出所有错误
检查压痕
我注意到(但可能只是粘贴到SO编辑器中)第1行(current_index=0
)的缩进在添加字段(编辑模板)和添加字段(新模板)文件中都出现了。CoffeeScript使用了严格的缩进规则:
# current_index = 0 # <- Your code has extra indentation here
current_index = 0
editText = ->
html = """
<div>
# ...
#当前索引=0#
html=”“”
# ...
用茧
否则,正如上面的评论中所提到的,一定要使用来保存大量的JS/CS样板文件。我还将添加到超级有用的工具列表中。更改
f.fields_for :thing
致:
因为它有很多联系
编辑:我还看到您的表单没有在服务器端呈现任何字段。但是,添加这些字段是必需的,rails才能显示您创建的关联:
正如其他人所建议的:使用现有的解决方案通过JS添加关联。这很棘手。试试我的嵌套表单:
我不知道cocoon,但它似乎也是一个很好的解决方案。我没有看到任何Java代码我的意思是不要按javascript-抱歉!请不要自己编写此javascript-你不会玩得很开心(你需要考虑很多事情)有一个非常棒的宝石,它就是专门为这个而写的:cocoon@BroiSatse-谢谢你的评论!我将使用cocoon gem我的嵌套形式场宝石也很好地用于此:非常感谢你的反馈克里斯-非常好的建议!非常棒的宝石Nico!非常感谢你的评论-完美!
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)
<%= form_for @post, html: { multipart: true } do |f| %>
<%= f.fields_for :thing do |ff| %>
<% end %>
<%= f.submit %>
<% end %>
# app/views/posts/_form.html.erb
<%= form_for @post, html: { multipart: true } do |f| %>
<%= f.fields_for :thing do |ff| %>
<% end %>
<%= f.submit %>
<% end %>
# app/views/posts/new.html.erb | app/views/posts/edit.html.erb
<button id="addtext">Text</button
<button id="addurl">URL</button
<%= render 'form' %>
$ tail -f log/development.log
# current_index = 0 # <- Your code has extra indentation here
current_index = 0
editText = ->
html = """
<div>
# ...
f.fields_for :thing
f.fields_for :things
<%= f.fields_for :things do |ff| %>
<%= ff.text_field :url %>
<%= ff.text_area :text %>
<% end %>