Rails: 我在上传文件时遇到了一个奇怪的问题 "NoMethodError"
Rails: I'm getting a strange "NoMethodError" when I upload files
我是 Rails 的新手,我似乎无法理解这一点。我有一个称为项目的基本 CRUD,人们可以在其中提交新项目。我也做到了,如果他们选择这样做,他们可以上传文件。现在文件上传完全正常,但问题是如果用户不上传文件我会收到此错误。 undefined method 'original_filename' for nil:NilClass
这行代码在这里 File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') do |file|
现在我有点难过了,因为我不需要original_filename
吗?添加文件时它可以工作,但是当没有文件时它不会...
projects_controller.rb
class ProjectsController < ApplicationController
def index
@projects = Project.all
end
def show
end
def new
@projects = Project.new
end
def create #no view
@projects = Project.new(project_params)
uploaded_io = params[:doc]
File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') do |file|
file.write(uploaded_io.read)
end
if @projects.save
redirect_to projects_path, :notice => "Your project was sent!"
else
render "new"
end
end
def edit
@projects = Project.find(params[:id])
end
def update #no view
@projects = Project.find(params[:id])
if @projects.update_attributes(project_params)
redirect_to projects_path, :notice => "Your project has been updated."
else
render "edit"
end
end
def destroy #no view
@projects = Project.find(params[:id])
@projects.destroy
redirect_to projects_path, :notice => "Your project has been deleted."
end
private
def project_params
params.require(:project).permit(:title, :description)
end
end
(项目)_form.html.erb
<%= form_for(@projects, :html => { :multipart => true}) do |f| %>
<div class="container">
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_area :description, class: "form-control" %>
</div>
<%= label_tag :doc, 'Files (optional)' %>
<%= file_field_tag :doc %>
<br>
<div class="form-group">
<%= f.submit "Submit Project", class: "btn btn-primary" %>
</div>
<% end %>
然后我还在 public
文件夹中设置了一个 uploads
文件夹。
只需进行以下更改:
def create #no view
@projects = Project.new(project_params)
uploaded_io = params[:doc]
if uploaded_io.present?
File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') do |file|
file.write(uploaded_io.read)
end
end
if @projects.save
redirect_to projects_path, :notice => "Your project was sent!"
else
render "new"
end
end
这将检查该文档是否存在,然后它只会尝试打开它,否则它会跳过它并且您不会收到错误。当没有文档时,params[:doc]
的值为 nil
。所以 rails 尝试做 nil.original_filename
抛出错误。
我是 Rails 的新手,我似乎无法理解这一点。我有一个称为项目的基本 CRUD,人们可以在其中提交新项目。我也做到了,如果他们选择这样做,他们可以上传文件。现在文件上传完全正常,但问题是如果用户不上传文件我会收到此错误。 undefined method 'original_filename' for nil:NilClass
这行代码在这里 File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') do |file|
现在我有点难过了,因为我不需要original_filename
吗?添加文件时它可以工作,但是当没有文件时它不会...
projects_controller.rb
class ProjectsController < ApplicationController
def index
@projects = Project.all
end
def show
end
def new
@projects = Project.new
end
def create #no view
@projects = Project.new(project_params)
uploaded_io = params[:doc]
File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') do |file|
file.write(uploaded_io.read)
end
if @projects.save
redirect_to projects_path, :notice => "Your project was sent!"
else
render "new"
end
end
def edit
@projects = Project.find(params[:id])
end
def update #no view
@projects = Project.find(params[:id])
if @projects.update_attributes(project_params)
redirect_to projects_path, :notice => "Your project has been updated."
else
render "edit"
end
end
def destroy #no view
@projects = Project.find(params[:id])
@projects.destroy
redirect_to projects_path, :notice => "Your project has been deleted."
end
private
def project_params
params.require(:project).permit(:title, :description)
end
end
(项目)_form.html.erb
<%= form_for(@projects, :html => { :multipart => true}) do |f| %>
<div class="container">
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_area :description, class: "form-control" %>
</div>
<%= label_tag :doc, 'Files (optional)' %>
<%= file_field_tag :doc %>
<br>
<div class="form-group">
<%= f.submit "Submit Project", class: "btn btn-primary" %>
</div>
<% end %>
然后我还在 public
文件夹中设置了一个 uploads
文件夹。
只需进行以下更改:
def create #no view
@projects = Project.new(project_params)
uploaded_io = params[:doc]
if uploaded_io.present?
File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') do |file|
file.write(uploaded_io.read)
end
end
if @projects.save
redirect_to projects_path, :notice => "Your project was sent!"
else
render "new"
end
end
这将检查该文档是否存在,然后它只会尝试打开它,否则它会跳过它并且您不会收到错误。当没有文档时,params[:doc]
的值为 nil
。所以 rails 尝试做 nil.original_filename
抛出错误。