使用 file_field 元素的 Rails 嵌套表单
我正在使用 Railscast 197 中找到的代码来使用 jQuery 创建嵌套表单。我面临的挑战是嵌套模型使用 file_field 元素,因此在查看 edit 视图时我只看到空 file_field 元素的列表。有没有办法让 Rails 在用户查看 edit 视图时显示其他内容(例如禁用的 text_field),但在查看 new 视图时显示 file_field?
这是我正在使用的部分:
<div class="fields">
<p>
<%= f.label :document_type %>
<%= f.select :document_type, XmlDocument::SOURCES %>
</p>
<p>
<%= f.file_field :inputfile, :class => 'text' %>
<%= f.hidden_field(:_delete) + link_to('Remove', "javascript:void(0)", :class => "remove_child remove-xmldoc") %>
</p>
</div>
这是添加它的 jQuery(对 Railscast 进行了稍微修改,以在显示部分时添加效果):
$('form a.add_child').click(function() {
var assoc = $(this).attr('data-association');
var content = $('#' + assoc + '_fields_template').html();
var regexp = new RegExp('new_' + assoc, 'g');
var new_id = new Date().getTime();
var newElements = jQuery(content.replace(regexp, new_id)).hide();
$(this).parent().before(newElements).prev().slideFadeToggle();
return false;
});
但是在我的 _form 部分中,我有:
<div id="xml_documents">
<%= label_tag 'XML Documents' %>
<% form.fields_for :xml_documents do |stream_xml_document_form| %>
<%= render :partial => "xml_document", :locals => {:f => stream_xml_document_form} %>
<% end %>
<p><%= link_to('Add XML Document', "javascript:void(0)", :class => "add_child add-xmldoc", :"data-association" => :xml_documents) %></p>
<%= new_child_fields_template(form, :xml_documents) %>
</div>
这就是添加部分的内容,我是猜测在这里的某个地方我需要有某种条件,告诉 Rails 在对象是否已经创建或未创建的情况下渲染不同的部分。任何人对我如何完成此任务有任何想法吗?
I am using code found at Railscast 197 to create nested forms using jQuery. The challenge I am facing is that the nested model uses a file_field element, and because of this when looking at the edit view I just see a list of empty file_field elements. Is there a way I can tell rails to display something else (like a disabled text_field) when the user is viewing the edit view, but a file_field when viewing the new view?
This is the partial I am using:
<div class="fields">
<p>
<%= f.label :document_type %>
<%= f.select :document_type, XmlDocument::SOURCES %>
</p>
<p>
<%= f.file_field :inputfile, :class => 'text' %>
<%= f.hidden_field(:_delete) + link_to('Remove', "javascript:void(0)", :class => "remove_child remove-xmldoc") %>
</p>
</div>
And this is the jQuery that adds it (modified slightly from the Railscast to add an effect when displaying the partial):
$('form a.add_child').click(function() {
var assoc = $(this).attr('data-association');
var content = $('#' + assoc + '_fields_template').html();
var regexp = new RegExp('new_' + assoc, 'g');
var new_id = new Date().getTime();
var newElements = jQuery(content.replace(regexp, new_id)).hide();
$(this).parent().before(newElements).prev().slideFadeToggle();
return false;
});
But within my _form partial I have:
<div id="xml_documents">
<%= label_tag 'XML Documents' %>
<% form.fields_for :xml_documents do |stream_xml_document_form| %>
<%= render :partial => "xml_document", :locals => {:f => stream_xml_document_form} %>
<% end %>
<p><%= link_to('Add XML Document', "javascript:void(0)", :class => "add_child add-xmldoc", :"data-association" => :xml_documents) %></p>
<%= new_child_fields_template(form, :xml_documents) %>
</div>
which is what adds the partial, I'm guessing that somewhere in here I need to have a conditional of some sort that tells rails to render a different partial if the object is already created or or not.. Anyone have any ideas as to how I can accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
区分“新建”和“编辑”操作的最简单方法是检查 form.object.new_record?这将为“新”操作返回 true,为“编辑”操作返回 false。
所以你可以这样做
The easiest way to distinguish between 'new' and 'edit' actions is to check for form.object.new_record? This will return true for 'new' action and false for 'edit' action.
So you could do i.e.