0

ユーザーが他のいくつかのフィールド (uploader_first_name、uploader_last_name、uploader_email) を含むフォームからビデオ ファイルをアップロードできる paperclip gem を使用して Rails 4 アプリを構築しています。

ペーパークリップのインストールはスムーズに進み (追加する必要がありましたが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 %>

Update 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/パスがディレクトリを指すようにしたくないですか?

更新 2 ビデオ モデルを次のように変更しました。

has_attached_file :file, :url=>"/tmp/video_uploads/:basename.:extension", :path=>":rails_root/tmp/video_uploads/:basename.:extension"

...そしてエラーはなくなりました。ファイルは正しいディレクトリに保存され、対応するフィールドが新しい行に追加されますが、他のすべてのフィールドはまだNULLです(元の問題)。

Update 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

私は何が起こっているのかを理解しました。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

Rails 4 では強力なパラメータに置き換えられましたが、それなしではペーパークリップを動作させることattr_accessibleができませんでした。誰かが私に方法を教えてくれるなら、私は知りたいです。

更新 私は完全に削除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 に答える
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 に答える