如何在erb Rails中提交之前需要使用file_field

时间:2015-05-10 09:12:16

标签: ruby-on-rails ruby ruby-on-rails-3 erb required

我正在尝试在使用Rails和ERB提交表单之前创建一个file_field,并且我正在绘制一个空白。

<%= form_for Image.new, html: {multipart: true} do |i| %>
  <%= i.label :description %>
  <%= i.text_field :description %>
  <%= i.file_field :image %>  <------**** This Line ****
  <%= i.submit "Upload" %>
<% end %>

我尝试过以各种方式使用'required'并在网上搜索但无济于事。这是我应该在模型中寻求验证的东西吗?

我的模特是这样的:

class Image < ActiveRecord::Base
  has_many :comments, dependent: :destroy
  has_many :likes, dependent: :destroy
  belongs_to :user

  has_attached_file :image, :styles => { :large => "600x600>", :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
  validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end

1 个答案:

答案 0 :(得分:2)

在建议答案列表的底部,我在stack thread

中找到了我要找的内容
<?PHP
//this part goes after the checking of the $_GET var
$fetch = mysql_query("SELECT * FROM table LIMIT $startrow, 4")or
die(mysql_error());
?>

我只需将其添加到我的模型中,它就不会让表单在没有图片附件的情况下提交。