Rails嵌套的带有图像的表格 [英] Rails Nested Forms With Images
问题描述
因此,我正在使用Campaigns模型和Sites模型(其中Campaigns具有多个站点)构建嵌套表单.在广告系列"表格中,我有:
So I'm building a nested form with an Campaigns model and a Sites model where Campaigns has_many Sites. In my Campaigns form I have:
<%= f.fields_for :sites do |builder| %>
<%= render "site_fields", :f => builder %>
<% end %>
然后在_site_fields.html.erb中,我拥有:
And then in the _site_fields.html.erb I have:
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<%= f.label "Image"%><br>
<%= f.file_field :image %>
<div class="field">
<%= f.label :url %><br>
<%= f.text_field :url %>
</div>
这一切似乎都起作用(令人震惊),但是我想预览已经为表单中的特定网站上传的图像.因此,在我有f.file_field:image的位置,我还希望能够显示该图像的预览.问题是我似乎无法访问正在渲染的当前站点,因为我正在使用f.fields_for.
This all seems to work (shockingly) but I would like to have a preview of the image already uploaded for a particular site in the form. So where I have f.file_field :image I would also like to be able to show a preview of that image. The problem is that I don't seem to be able to access the current site being rendered because I'm using f.fields_for.
有什么建议吗?我确定我缺少相对简单的东西.
Any suggestions? I'm sure I'm missing something relatively simple.
提前谢谢!
推荐答案
从表单帮助器对象(在您的情况下为 builder 和 f ),您应该可以访问模型对象和您要查找的网址,如下所示:
From the form helper object (in your case builder and f) you should be able to access the model object and the url your looking for like this:
<%= image_tag f.object.image_url(:thumb) %>
image _url中的 image 取决于您如何命名属性,但是对于您的示例来说,这应该是正确的.
The image in image_url depends on how you named the attribute, but for your sample this should be correct.
这篇关于Rails嵌套的带有图像的表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!