2016-09-05 19 views
0

RoRとSimple_formの新機能です。私は2つのクラスの間に関連があるシンプルなセットアップをしています。私が使用しているフォームは更新/保存ではなく、常に値を空白に戻しています。ドキュメントやその他の投稿を見て、私は間違って何をしていますか?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> 
+0

コントローラの動作のどこかで '@ annotation.save'を使用していたと思いますが、代わりに' @ annotation.save! 'を使用して検証エラーを発生させます。こうすることで、どのようなエラーがオブジェクトの保存を妨げているのかを知ることができます。 –

+0

venkat、...に変更しようとしました!解決しなかった –

答えて

2

です。 annotation_params:documenttype_idを追加する必要がありました。

関連する問題