Rails简单形式关联不保存

时间:2016-09-05 12:05:50

标签: ruby-on-rails simple-form

是RoR和Simple_form的新手。我有一个简单的设置,我有两个类之间的关联。我使用的表单不​​更新/保存,并始终将值设置为空白。看看文档和其他帖子,我做错了什么?

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>

1 个答案:

答案 0 :(得分:2)

我找到了解决方案;我需要将:documenttype_id添加到annotation_params