我正在使用回形针 gem 构建一个 Rails 4 应用程序,用户可以在其中从具有其他几个字段(uploader_first_name、uploader_last_name、uploader_email)的表单上传视频文件。
回形针安装顺利(虽然我需要添加gem 'protected_attributes'
),我能够将文件保存到正确的位置并在视频表中创建相应的记录,但是所有非回形针字段都是null
,我不知道为什么。
class Video < ActiveRecord::Base
belongs_to :project
attr_accessible :file
has_attached_file :file, :url=>"/tmp/video_uploads", :path=>":rails_root/tmp/video_uploads"
end
class VideosController < ApplicationController
def save
@video = Video.create(params[:video])
if @video.save
redirect_to root_path
else
redirect_to "somewhere_else"
end
end
#I tried with this too...
#private
#def video_params
#params.require(:video).permit( :uploader_first_name, :uploader_last_name, :uploader_email)
#end
end
在视图中...
<h2>Upload your video</h2>
<% myclass = {:class=>'form-control'} %>
<%= form_for(:video, :url => {:action=>'save', :controller=>'videos'}, :html=>{:class=>'upload-form-js', :multipart=> true} ) do |f| %>
<div class="form-group">
<%= f.label(:uploader_first_name, "First Name") %>
<%= f.text_field(:uploader_first_name, myclass) %>
</div>
<div class="form-group">
<%= f.label(:uploader_last_name, "Last Name") %>
<%= f.text_field(:uploader_last_name, myclass) %>
</div>
<div class="form-group">
<%= f.label(:uploader_email, "Email") %>
<%= f.text_field(:uploader_email, myclass) %>
</div>
<div class="form-group">
<label>Video File</label>
<input type="file" name="video[file]" id="video_file"/></span>
</div>
<%= f.submit('Upload', {class: 'btn btn-primary btn-block'}) %>
<% end %>
更新1 我改成这个...
class VideosController < ApplicationController
def save
@video = Video.create( video_params )
if @video.save
redirect_to root_path
else
redirect_to "somewhere_else"
end
end
private
def video_params
params.require(:video).permit(:file, :uploader_first_name, :uploader_last_name, :uploader_email)
end
end
...现在我收到此错误:
Errno::EISDIR in VideosController#save
Is a directory - /Applications/MAMP/htdocs/clippo2/tmp/video_uploads
我不想让 url/path 指向一个目录吗?
更新 2 我将视频模型更改为...
has_attached_file :file, :url=>"/tmp/video_uploads/:basename.:extension", :path=>":rails_root/tmp/video_uploads/:basename.:extension"
...现在没有错误,文件被保存到正确的目录,并且相应的字段添加到新行,但所有其他字段仍然为 NULL(原始问题)。
更新 3 我打开了调试器,这是我尝试上传文件后看到的内容。它看起来像一个强参数错误,但我不确定如何修复它:
Started POST "/videos/save" for 127.0.0.1 at 2013-08-27 09:21:34 -0400
Processing by VideosController#save as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"es4wPqFr9xPBUFsbHQR/gAzofDC+ZwYsiiJ7RAQZUHk=", "video"=>{"uploader_first_name"=>"adsfasdf", "uploader_last_name"=>"asdfasdf", "uploader_email"=>"asdfasdf", "file"=>#<ActionDispatch::Http::UploadedFile:0x007fc4782e31e8 @tempfile=#<Tempfile:/var/folders/f2/jhv7xx0j3hlckhcg_jbv6hr00000gn/T/RackMultipart20130827-89636-188f0hs>, @original_filename="sample_iPod.m4v", @content_type="video/mp4", @headers="Content-Disposition: form-data; name=\"video[file]\"; filename=\"sample_iPod.m4v\"\r\nContent-Type: video/mp4\r\n">, "project_hashed_id"=>"1377539908"}, "commit"=>"Upload"}
{"utf8"=>"✓", "authenticity_token"=>"es4wPqFr9xPBUFsbHQR/gAzofDC+ZwYsiiJ7RAQZUHk=", "video"=>{"uploader_first_name"=>"adsfasdf", "uploader_last_name"=>"asdfasdf", "uploader_email"=>"asdfasdf", "file"=>#<ActionDispatch::Http::UploadedFile:0x007fc4782e31e8 @tempfile=#<Tempfile:/var/folders/f2/jhv7xx0j3hlckhcg_jbv6hr00000gn/T/RackMultipart20130827-89636-188f0hs>, @original_filename="sample_iPod.m4v", @content_type="video/mp4", @headers="Content-Disposition: form-data; name=\"video[file]\"; filename=\"sample_iPod.m4v\"\r\nContent-Type: video/mp4\r\n">, "project_hashed_id"=>"1377539908"}, "commit"=>"Upload", "controller"=>"videos", "action"=>"save"}
Unpermitted parameters: project_hashed_id
WARNING: Can't mass-assign protected attributes for Video: uploader_first_name, uploader_last_name, uploader_email
app/controllers/videos_controller.rb:6:in `save'
[1m[35m (0.2ms)[0m BEGIN
[1m[36mSQL (0.3ms)[0m [1mINSERT INTO `videos` (`created_at`, `file_content_type`, `file_file_name`, `file_file_size`, `file_updated_at`, `updated_at`) VALUES ('2013-08-27 13:21:34', 'video/mp4', 'sample_iPod.m4v', 2236480, '2013-08-27 13:21:34', '2013-08-27 13:21:34')[0m
[1m[35m (9.0ms)[0m COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 21ms (ActiveRecord: 9.5ms)