2013-08-26 41 views
0

我正在構建一個Rails 4應用程序與回形針寶石,其中用戶可以從一個窗體上傳視頻文件與其他幾個字段(uploader_first_name,uploader_last_name,uploader_email) 。回形針寶石與Rails 4不保存非文件字段

回形針安裝進展順利(雖然我需要添加gem 'protected_attributes' ),我能保存文件到正確的,並創建相應的記錄在視頻表,但是所有的 -paperclip字段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 /路徑指向一個目錄嗎?

更新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) 
+0

當你說你「嘗試」了註釋掉的代碼時,你究竟是如何整合它的? –

+0

我取消註釋並用'Video.create(video_params)'替換'Video.create(params [:video])''。這並沒有拋出錯誤,但它導致文件不被保存,也沒有導致其他字段被保存。 – emersonthis

+0

好吧,如果沒有'require',那麼你不會得到這些字段,如果'save'失敗,你不會得到_any_字段。所以你需要保留'require',我想,並且在這種情況下找出'save'失敗的原因。 –

回答

0

歡迎導軌4「attr_accessible」是由強參數取代。
http://api.rubyonrails.org/classes/ActionController/StrongParameters.html

Upd。你可以試試這個嗎?

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 
+0

您是否看到註釋掉的代碼? –

+0

你沒錯,沒看見。 – x3qt

+0

@ x3qt我注意到你的'.permit()'包含':file'。我想知道我應該試試嗎? – emersonthis

0

我想清楚發生了什麼事情。因爲我在模型中使用了attr_accessible,所以強大的參數正在實現。相反,與需要擺弄()和許可()的我刪除他們完全並添加缺少的字段到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在軌道4,5但是我換成了強大的參數沒有它,不能讓回形針工作。如果有人能告訴我如何,我很想知道。

更新 我刪除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創業板和重啓rails s它生效(這個錯誤的n00b我花了45分鐘!)

這個故事告訴我們:不要混搭attr_accessible與強烈的參數。做一個或另一個和回形針工作與強大的參數。

+0

你有沒有嘗試取出attr_accessible並留下強大的參數? –

+1

它似乎工作,根據http://stackoverflow.com/questions/17737709/paperclip-in-rails-4-strong-parameters-forbidden-attributes-error。 :-) –

+0

@PeterAlfvin我會嘗試一下。 – emersonthis