0
RoR和Simple_form的新功能。我有一個簡單的設置,我有兩個類之間的關聯。我使用的表單不是更新/保存,而是始終將該值設置爲空白。看着文檔和其他帖子,我做錯了什麼?Rails簡單表單關聯不保存
類
class Annotation < ApplicationRecord
has_many :comments, dependent: :destroy
belongs_to :documenttype
has_attached_file :file, styles: { large: "600x600>", medium: "500x500>", thumb: "150x150#" }, default_url: "/images/:style/missing.png"
accepts_nested_attributes_for :documenttype
validates_attachment_content_type :file, content_type: ['image/jpeg', 'image/png', 'image/gif', 'application/pdf']
validates :name, presence: true, uniqueness: true, length: { minimum: 10, maximum: 50 }
validates :description, length: { minimum: 20, maximum: 500 }
validates :documenttype, presence: true
validates :file, presence: true
end
class Documenttype < ApplicationRecord
has_many :annotations
validates :name, presence: true, uniqueness: true, length: { minimum: 5 }
end
PARAMS
def annotation_params
params.require(:annotation).permit(:name, :description, :file, :active, :documenttype)
end
def documenttype_params
params.require(:documenttype).permit(:name, :description, :active, annotation_attributes: [:id, :name])
end
這是形式...
<div class="container-fluid">
<div class="row">
<div class="col-md-6">
<%= simple_form_for @annotation, html: { class: 'form-horizontal', multipart: true },
wrapper: :horizontal_form,
wrapper_mappings: {
check_boxes: :horizontal_radio_and_checkboxes,
radio_buttons: :horizontal_radio_and_checkboxes,
file: :horizontal_file_input,
boolean: :horizontal_boolean
} do |f| %>
<%= f.error_notification %>
<%= f.input :name, placeholder: 'Enter name' %>
<%= f.input :description, placeholder: 'Description' %>
<%= f.association :documenttype %>
<%= f.input :active, as: :boolean %>
<% if @annotation.file.blank? %>
<%= f.input :file, as: :file %>
<% else %>
<% end %>
<%= f.button :submit %>
<% unless @annotation.file.blank? %>
<%= link_to ' Annotate', annotations_path, :class => "btn btn-default" %>
<% end -%>
<% end %>
<p><br><%= link_to 'List' , annotations_path %></p>
</div>
<div class="col-md-6">
<% unless @annotation.file.blank? %>
<%= image_tag @annotation.file.url(:large) %>
<% end %>
</div>
</div>
我想,在控制器動作的某個地方,您可能使用了@ annotation.save而不是使用@ annotation.save!來引發驗證錯誤。這樣你就會知道什麼是防止對象保存的錯誤。 –
venkat,試圖改變...創造!沒有解決 –