2014-12-30 13 views
0

我在軌這段代碼:上傳照片彷彿回到零,original_filename不能稱爲

控制器:

def new 
    end 

    def create 
     the_file = params[:upload][:picture] 
     @user = User.where("first_name = ?", session[:current_user_id]).first 
     @photo = Photo.create(:date_time => DateTime.now, :file_name => the_file.original_filename, :user_id => @user.id) 
     directory = "app/assets/images" 
     path = File.join(directory, the_file.original_filename) 
     File.open(path, "wb") do |f| 
      f.write(the_file.read) 
     end 
     redirect_to "photos/index/" + @user.id.to_s 
    end 

和視圖:

<% if session[:current_user_id] == nil%> 
    <p class = "error" >You cannot upload a photo because you are not logged in. Please log in to continue.</p> 
    <% else %> 
    <p> 
    <%= form_tag({action: :create}, multipart: true) do %> 
    <%= file_field_tag 'upload[picture]' %> 
    <%= submit_tag("Upload") %> 
    <% end %> 
    <% end %> 

現在,我得到一個未定義的方法`[]」的零:NilClass錯誤,可能是因爲第二個[:畫面]正在呼籲爲零。我已經嘗試了params [:photo] [:upload]等幾乎所有的變體,但它似乎總是將參數視爲零,或返回一個長字符串(ActionController ...的東西,但original_filename不能被調用它)。任何關於正在發生的事情的想法都會被讚賞!我懷疑這張照片實際上沒有上傳,或者我的參數是錯誤的 - 我只是不知道在哪裏。

+0

這是軌道3或4?你如何處理大規模的任務? –

+0

這是導軌4.我不確定你的意思是批量分配 - 這可能是問題的關鍵。 – cat

+0

剛剛搜索起來 - 因爲這是軌道4,它不應該是問題的權利? – cat

回答

0

由於我沒有評論你的帖子的聲望,我不得不把這個作爲答案。我正在查看file_field_tag的示例,它沒有提及您在使用'upload [picture]'的視圖中使用的語法,然後嘗試將它作爲參數中的散列來訪問。我會嘗試使用以下的觀點:
file_field_tag 'picture'
並在控制器中使用:
the_file = params[:picture]

here is the link to the reference I mentioned