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不能被調用它)。任何關於正在發生的事情的想法都會被讚賞!我懷疑這張照片實際上沒有上傳,或者我的參數是錯誤的 - 我只是不知道在哪裏。
這是軌道3或4?你如何處理大規模的任務? –
這是導軌4.我不確定你的意思是批量分配 - 這可能是問題的關鍵。 – cat
剛剛搜索起來 - 因爲這是軌道4,它不應該是問題的權利? – cat