0

我正在使用回形针 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)
4

2 回答 2

0

欢迎使用 rails 4. 'attr_accessible' 被替换为强参数。
http://api.rubyonrails.org/classes/ActionController/StrongParameters.html

更新。你能试试这个吗?

def create
  @video = Video.create(video_params)
end

private

def video_params
  params.require(:video).permit(:file, :uploader_first_name, :uploader_last_name, :uploader_email)
end
于 2013-08-26T21:37:14.790 回答
0

我弄清楚发生了什么事。因为我attr_accessible在模型中使用了强大的参数。而不是摆弄 require() 和 permit() 我完全删除了它们并将缺少的字段添加到 attr_accessible ,现在它可以工作了:

class Video < ActiveRecord::Base
    belongs_to :project
    attr_accessible :file, :uploader_first_name, :uploader_last_name, :project_hashed_id, :uploader_email, :rating
    has_attached_file :file, :url=>"/tmp/video_uploads/:basename.:extension", :path=>":rails_root/tmp/video_uploads/:basename.:extension"
end

class VideosController < ApplicationController
  def save
    logger.debug( params )
    #@video = Video.new( video_params )
    @video = Video.new( params[:video])
    if @video.save
        redirect_to root_path
    else
        redirect_to "somewhere_else"
    end
  end
end

我意识到这attr_accessible已被 Rails 4 中的强大参数所取代,但没有它我无法让回形针工作。如果有人能告诉我怎么做,我很想知道。

更新 我完全删除attr_accessible了,只是使用了强参数......

class VideosController < ApplicationController

  def save
    logger.debug( params )
    @video = Video.new( 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, :project_hashed_id)
      params.require(:video).permit!
    end

end

...它可以工作,但你必须记住删除 protected_attributes gem 并重新启动rails s它才能生效(这个 n00b 错误花了我 45 分钟!)

这个故事的寓意:不要attr_accessible与强大的参数混搭。做一个或另一个,回形针使用强大的参数。

于 2013-08-27T13:56:44.410 回答