這是我一起試圖讓Carrierwave從觀看Railscast開始工作。我有一個Post Project頁面,用戶可以在其中輸入項目的詳細信息。他們也可以上傳文件到這個頁面,並提交項目。所以我在頁面上使用了nested_form_for。RoR:Carrierwave無法正常工作/保存到數據庫
new_step_3.html.erb
<%= nested_form_for @project, :html => {:multipart => true} do |f| %>
<%= f.text_field :title %>
<%= f.text_field :description %>
<%= f.fields_for :document do |attachment_form| %>
<%= attachment_form.file_field :title %>
<% end %>
<%= f.text_field :skills %>
<%= f.submit 'Post Project' %>
<% end %>
project.rb模型
attr_accessible :category, :title, :budget, :end_date, :description, :skills, :document, :days_lasting, :documents_attributes
belongs_to :user
has_many :posts
has_many :documents, :as => :attachable
validates_presence_of :category, :title, :description, :skills
accepts_nested_attributes_for :documents
document.rb模型
attr_accessible :project_id, :title, :document
belongs_to :user
belongs_to :project
has_many :posts
mount_uploader :document, DocumentUploader
projects_controller.rb
def create
@project = current_user.projects.build(params[:project])
respond_to do |format|
if @project.save
format.html { redirect_to project_step_4_path(:start => @project.id), notice: 'Project was successfully created.' }
format.json { render json: @project, status: :created, location: @project }
else
format.html { render action: "new" }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
末
現在,當我嘗試提交表單,它會說未知屬性:文件 應用程序/控制器/ projects_controller.rb:85:在'創造」
運行Rails中的命令控制檯進行Document.create!(:文件=> File.new( 「test.jpg放在」))
請問您的底層數據庫架構必須在「文件」表中的VARCHAR「文件」列? – clyfe 2011-12-19 23:02:55
它確實,因爲他說它在控制檯中工作正常;) – Robin 2011-12-19 23:07:33