嵌套属性(Paperclip)验证

时间:2013-10-12 05:08:54

标签: ruby-on-rails validation ruby-on-rails-4 paperclip nested-forms

我正在处理嵌套表单,其中用户在创建屏幕时还应附加图像(名为屏幕截图的单独模型)。在将新屏幕保存到数据库之前,我正在尝试验证附件的存在。我尝试在屏幕截图模型中验证附件的存在,但只保留了屏幕截图,同时它仍然创建了一个屏幕。

这是我的模特:

class Screen < ActiveRecord::Base

  belongs_to :project
  has_many :screenshots

  validates :name, presence: true

  accepts_nested_attributes_for :screenshots

  validates_presence_of :screenshots 

end


class Screenshot < ActiveRecord::Base

  belongs_to :screen

  has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }

end

这是我的控制器:

def create
    @screen = Screen.create(screen_params)
    if @screen.save
      flash[:notice] = "A new screen has been added to this project"
      redirect_to [@screen.project]
    else
      render :action => 'new'
    end
  end

最后这是我的表格:

<%= form_for ([@project, @screen]), :html => { :multipart => true } do |f| %>
  <% if @screen.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@screen.errors.count, "error") %> prohibited this screen from being saved:</h2>

      <ul>
      <% @screen.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.hidden_field :project_id %>
  </div>
  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :description %><br>
    <%= f.text_field :description %>
  </div>

  <%= f.fields_for :screenshots, @screen.screenshots.build do |s| %>
    <%= s.hidden_field :screen_id, :value => @screen.id %>
    <%= s.hidden_field :version, :value => "1" %>
    <%= s.label :image %><br>
    <%= s.file_field :image %>
  <% end %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

非常感谢任何对这个菜鸟的帮助。

1 个答案:

答案 0 :(得分:0)

适合您的一些事情:

<强>表格 您不应该在表单本身中构建新对象。这需要在控制器的new函数

中进行
 <%= f.fields_for :screenshots do |s| %>
    <%= s.hidden_field :screen_id, :value => @screen.id %>
    <%= s.hidden_field :version, :value => "1" %>
    <%= s.label :image %><br>
    <%= s.file_field :image %>
  <% end %>

您可以这样声明:

def new
    @screen = Screen.new
    @screen.screenshots.build
    #any more build declarations go here
end

<强>验证 我相信您应该将验证放入screnshots模型中,因为嵌套参数会直接传递给相应的模型。虽然我不确定。

在屏幕截图模型中,我会将validates_presence_of :screenshots与名称作为您的回形针附件的名称