Rails 3.0 - form_for 和当前模型之外的字段
当我向 Rails 3.0 照片库站点提交照片时。除了能够上传文件、从下拉列表中选择相册之外,我还需要能够传递逗号分隔标签的列表。我对 form_for 的正确使用感到困惑。注意:这是在 new.html.erb 视图(以及 PhotoController 的新方法)中,
<%= form_for(@photo) do |f| %>
<% if @photo.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@photo.errors.count, "error") %> prohibited this photo from being saved:</h2>
<ul>
<% @photo.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label :album_id %>
<%= select "photo", "album_id", @albums.map {|a| [a.name,a.id]} %><br />
<%= f.label :name %>
<%= f.text_field :name %><br />
<%= f.label :photofile %>
<%= f.file_field :photofile %><br />
<%= f.label :tags %>
<%= f.text_field :tags %><br />
<%= f.submit %>
<% end %>
我对 (1) 上传文件的正确方法感到困惑 - 我是否使用 f.file_field 沿着正确的路径前进。 photofile 实际上并不是 Photo 对象的属性。 (2) 与 :tags 类似的问题 - 标签不是 Photo 对象的属性,所以它应该是 f.text_field (3) 我假设我执行相册下拉列表的方式是正确的(我希望?)
When I submit a Photo to a Rails 3.0 photo gallery site. In addition to being able to upload the file, select Album from a drop down list, I need to be able to pass along a list of comma delimited tags. I'm confused about the proper use of form_for. note: this is in the new.html.erb view (and new method of the PhotoController)
<%= form_for(@photo) do |f| %>
<% if @photo.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@photo.errors.count, "error") %> prohibited this photo from being saved:</h2>
<ul>
<% @photo.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label :album_id %>
<%= select "photo", "album_id", @albums.map {|a| [a.name,a.id]} %><br />
<%= f.label :name %>
<%= f.text_field :name %><br />
<%= f.label :photofile %>
<%= f.file_field :photofile %><br />
<%= f.label :tags %>
<%= f.text_field :tags %><br />
<%= f.submit %>
<% end %>
I'm confused about the proper way that I can (1) upload a file - am I headed down the right path with f.file_field. photofile isn't really a property of the Photo object. And (2) similar question with :tags - tags isn't a property on the Photo object so should it be f.text_field (3) I'm assuming the way I am doing the Album dropdown is correct (I hope?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请记住,您可以在 form_for 内创建任何字段,就像任何其他表单一样。
该表单只是通过生成名称如
photo[name]
的字段来帮助您但您可以使用
text_field_tag :tags
并在控制器上使用params[:tags]
检索它,没有问题。如果该字段不是来自您的模型,则不应使用
f.
,而应使用另一个适合您的数据的帮助器,例如text_field_tag
,甚至是纯 html(如果)你愿意。Remember you can create any fields inside the form_for, just like any other form.
The form for just helps you by generating fields with names like
photo[name]
but you can use a
text_field_tag :tags
and on the controller retrieve it withparams[:tags]
without a problem.If the field is not from your model, you shouldn't use the
f.<helper>
just use another helper liketext_field_tag
appropriate for your data or even pure html if you'd like.首先,你需要说:multipart => true 在你的 form_for 调用中。这表示该表格具有附件。然后,对于附件,请使用 paperclip 或 载波。查看这些 Railscast,了解如何使用 paperclip 和 Carrierwave。
对于标记此railscast是一个有用的资源。
Firstly, you need to say :multipart => true in your form_for call. This says that the form has attachment. Then, for attachments, use a gem like paperclip or carrierwave. Checkout these railscasts for a nice introduction on how to use paperclip and carrierwave.
For tagging this railscast is a useful resource.