---
title: "Rails4.2 ActiveAdmin使用"
author: "hnus"
date: "2015-03-27T15:55:00Z"
tags: []
slug: "rails4-2-activeadmin"
draft: false
lastmod: "2015-05-11T11:49:41Z"
id: 2210119
url: "https://hnus.iteye.com/blog/2210119"
---

表单使用上传的写法

form :html =&gt; { :multipart =&gt; true } do |f|

end

form :html =&gt; { :enctype =&gt; "multipart/form-data"} do |f|

end

文件上传：

```
#file is virtual  attribute
```

```
 def file= file
  p "file.class is : " ,file.class
  doc_type = file.original_filename.split('.').last
  location = File.join(STORE_DIR, file.original_filename)
  self.audio_file = location 
  File.open(File.join(STORE_PATH, file.original_filename), "wb") { |f| f.write(file.read) }
 end
```

app/admin/xxxmodel.rb

```
  form :html => { :multipart => true } do |f|
    f.inputs "DoubleCharacter" do
      f.input :name ,:label=>"name"
      f.input :consonant1  ,:label=>"consonant1"
      f.input :vowels1  ,:label=>"vowels1"
      f.input :tone1  ,:label=>"tone1"
      f.input :consonant2  ,:label=>"consonant2"
      f.input :vowels2  ,:label=>"vowels2"
      f.input :tone2  ,:label=>"tone2"
      # f.input :audio_file  ,:label=>"audio_file"
      #f.file_field :audio_file,:label=>"audio_file"
      f.file_field :file,:label=>"audio_file"
    end
    f.submit
    end
```

Must Be Careful

permit\_params:name, :consonant1, :vowels1, :tone1 , :consonant2, :vowels2, :tone2, :audio\_file,:file

More Info : [ActiveAdmin/issues](https://github.com/activeadmin/activeadmin/issues/3476)