【发布时间】:2019-01-16 22:29:17
【问题描述】:
所以我正在开发我的应用程序并尝试添加上传功能。
首先我生成了一个课程上传器,并允许使用 jpeg、jpg、png 和 gif。
接下来我在我的课程模型上添加了 ff 代码挂载:
mount_uploader :thumb_image, CoursesUploader
mount_uploader :main_image, CoursesUploader
接下来,我将文件上传表单代码放在我的 new.html.erb 文件中:
<div class="field">
<%= f.file_field :main_image %>
</div>
<div class="field">
<%= f.file_field :thumb_image %>
</div>
当我尝试提交带有上传图片的表单时,它甚至没有显示索引页面,而是刷新页面,然后查看它是否已上传并且表单是否已成功提交,但没有。
我还查看了我的控制器上的强参数,这两个项目在那里:
# Never trust parameters from the scary internet, only allow the white list through.
def course_params
params.require(:course).permit(:title, :price, :body, :main_image, :thumb_image)
end
知道我在这里缺少什么吗?
这是我提交后从终端收到的内容:
Started POST "/courses" for 127.0.0.1 at 2019-01-16 20:52:20 +0800
Processing by CoursesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"wlKpv8czsuC6AZjCctxYk203wry4cb0dnOYI9IoAkzRZNV2vB4/vREU5L5u4G7vh5ZhCbHZlJ6nHPN95qO+cbA==", "course"=>{"title"=>"Goody", "price"=>"33", "main_image"=>#<ActionDispatch::Http::UploadedFile:0x007f81e52eed48 @tempfile=#<Tempfile:/var/folders/3_/8bmsd3j13bxfdl1jp7gzvvsm0000gn/T/RackMultipart20190116-2359-17w6pb.jpg>, @original_filename="61230.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"course[main_image]\"; filename=\"61230.jpg\"\r\nContent-Type: image/jpeg\r\n">, "thumb_image"=>#<ActionDispatch::Http::UploadedFile:0x007f81e52eecd0 @tempfile=#<Tempfile:/var/folders/3_/8bmsd3j13bxfdl1jp7gzvvsm0000gn/T/RackMultipart20190116-2359-2mcy4i.jpg>, @original_filename="61230.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"course[thumb_image]\"; filename=\"61230.jpg\"\r\nContent-Type: image/jpeg\r\n">, "body"=>""}, "commit"=>"Create Course"}
(0.5ms) BEGIN
↳ app/controllers/courses_controller.rb:27
Course Exists (1.5ms) SELECT 1 AS one FROM "courses" WHERE "courses"."id" IS NOT NULL AND "courses"."slug" = $1 LIMIT $2 [["slug", "goody"], ["LIMIT", 1]]
↳ app/controllers/courses_controller.rb:27
(0.6ms) ROLLBACK
↳ app/controllers/courses_controller.rb:27
Rendering courses/new.html.erb within layouts/application
Rendered courses/new.html.erb within layouts/application (12.6ms)
Rendered shared/_application_nav.html.erb (6.2ms)
Rendered shared/_application_footer.html.erb (1.3ms)
Completed 200 OK in 394ms (Views: 356.6ms | ActiveRecord: 2.6ms)
更新:完整模型内容
class Course < ApplicationRecord
include DefaultsConcern
enum status: { draft: 0, published: 1 }
validates_presence_of :title, :price, :body, :main_image, :thumb_image
mount_uploader :thumb_image, CoursesUploader
mount_uploader :main_image, CoursesUploader
extend FriendlyId
friendly_id :title, use: :slugged
end
完整的内容:
<h1>New Form</h1>
<%= form_for(@course) do |f| %>
<div class="field">
<%= f.label :title %>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :price %>
<%= f.number_field :price %>
</div>
<div class="field">
<%= f.file_field :main_image %>
</div>
<div class="field">
<%= f.file_field :thumb_image %>
</div>
<div class="field">
<%= f.label :body %>
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
【问题讨论】:
-
你能展示课程模型吗?
-
main_image 和 thumb_image 应该是不同的图像?还是相同但大小不同?
-
更新了我的答案并显示了完整的模型。 atGabbar atNEL 这次只是免费尺寸
标签: ruby-on-rails