我正在构建我的第一个Rails应用程序,直到现在一切正常,但后来我发现以下场景:一个演示文稿应该有N个迭代。我没有使用REST。所以,我试图做一个简单的表单来创建迭代。Rails 3:保存方法不会工作
这些机型:
class Presentation < ActiveRecord::Base
has_many :iterations
end
class Iteration < ActiveRecord::Base
belongs_to :presentation
attr_accessible :presentation_id, :description, :delivery_date, :file
validates :presentation_id, :presence => {:message => 'is required.'}
end
这些都是在控制器的动作:
#Shows Form
def add
@iteration = Iteration.new
@presentation = Presentation.find(params[:id])
end
#Saves Form
def save
@iteration = Iteration.new(params[:iteration])
@iteration.delivery_date = Time.now
if @iteration.save
flash[:notice] = "Saved succesfully!"
else
flash[:error] = "Changes were not saved."
end
redirect_to root_url
end
这将是HAML的观点:
= form_for @iteration, :url => { :action => "save", :method => "post" }, :html => { :multipart => true } do |f|
- if @iteration.errors.any?
There were some errors:
.notice-text.fg-color-white
%ul.notice
- for message in @iteration.errors.full_messages
%li= message
%br
.field
= f.label :description, "Description"
= f.text_area :description, :class=>"form-text-area", :rows=>5
.field
= f.label :file, "Upload File"
= f.file_field :file
.field
= hidden_field_tag :presentation_id, @presentation.id
%br
= f.submit "Save"
问题是,保存方法不会保存,但@ iteration.errors.count的视图上的值是0. 我用然后保存!相反,当我在另一篇文章中读到时,以这种方式抛出以下错误:
验证失败:需要演示。
我搞不清楚我做错了什么。请注意,在我曾经有过“f.hidden_field”而不是“hidden_field_tag”的视图中,但是出于其他一些原因我改变了它,但是在此之前我得到了同样的错误。
我注意到没有任何属性到达控制器... – 2013-03-17 20:34:00