2016-09-05 4 views
0

Bin neu in RoR und Simple_form. Ich habe eine einfache Einrichtung, wo ich eine Verbindung zwischen 2 Klassen habe. Das Formular, das ich verwende, wird nicht aktualisiert/gespeichert und der Wert wird immer auf leer zurückgesetzt. Betrachtete die Dokumente und andere Postings, was mache ich falsch?Rails Simple-Form-Zuordnung nicht speichern

Klassen

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 

Dies ist die Form ...

<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> 
+0

Ich vermute, irgendwo in der Controller-Aktion haben Sie '@ annotation.save' verwendet, stattdessen verwenden Sie' @ annotation.save! ', Um die Validierungsfehler zu erhöhen. Auf diese Weise erfahren Sie, welche Fehler das Speichern von Objekten verhindern. –

+0

Venkat, versucht zu ändern ... zu erstellen! löste sich nicht –

Antwort

2

ich die Lösung gefunden; Ich musste :documenttype_id zu annotation_params hinzufügen.

Verwandte Themen