我在使用ruby 2.3.0和CarrierWave在Rails 4.2.6中上传图像时遇到问题。
我有一个播客模型,当我尝试从表单创建新模型,提供名称,说明和.jpg图像时,我收到以下错误:Missing template podcasts/create, application/create with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/Users/mychro94/Desktop/Prova/app/views" * "/Users/mychro94/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/devise-i18n-1.6.1/app/views" * "/Users/mychro94/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/devise-4.4.3/app/views"
如果我删除
中的:image字段def podcast_params
params.require(:podcast).permit(:name, :description, :image)
end
我不再收到该错误,但图片未添加到创建的对象中。
播客创建方法是:
def create
@podcast = current_user.podcasts.create(podcast_params)
if @podcast.save
redirect_to podcast_path(@podcast)
end
end
和new.html.erb文件是
<%= form_for(@podcast, html: { :multipart => true, id: "input- container", class: "cf wrapper"}) do |f| %>
<div id="image-upload">
<%= f.file_field :image, :required => true %>
<!--<label for="image" class="inputfile-label">Choose a file</label>-->
</div>
<div id="title">
<%= f.text_field :name,:required => true , placeholder: "Insert Podcast title", class: "border-animation" %>
</div>
<div id="description" >
<%= f.text_area :description, :required => true , placeholder: "Insert Podcast description", class: "border-animation"%>
</div>
<div class="btn-bar">
<div class="right">
<%= f.submit "Create my podcast",class: "button button-highlight" %>
</div>
</div>
<% end %>
这是我在创建播客时在终端中获得的内容
Started POST "/podcasts" for ::1 at 2018-04-04 09:24:00 +0200
Processing by PodcastsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"wE57eKfcn/74znzOaQ4SBdYf5PSxPFBOZou9EOrhLNHmBzDNBa/nuw7+PJhIa5Lzqb3g55uIjqdhYmrSko+Wew==", "podcast"=>{"image"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fc9043f4fd8 @tempfile=#<Tempfile:/var/folders/7y/kh7t8w9j2tzf54_vh32st63r0000gn/T/RackMultipart20180404-50589-qd7z9c.jpg>, @original_filename="pod.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"podcast[image][image]\"; filename=\"pod.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "name"=>"asdas", "description"=>"asdasd"}, "commit"=>"Create my podcast"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Unpermitted parameter: image
(0.1ms) begin transaction
SQL (0.4ms) INSERT INTO "podcasts" ("name", "description", "artist_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?) [["name", "asdas"], ["description", "asdasd"], ["artist_id", 1], ["created_at", "2018-04-04 07:24:00.672689"], ["updated_at", "2018-04-04 07:24:00.672689"]]
(0.9ms) commit transaction
(0.1ms) begin transaction
(0.0ms) commit transaction
Redirected to http://localhost:3000/podcasts/28
Completed 302 Found in 11ms (ActiveRecord: 1.8ms)
确定这个
有问题"podcast"=>{"image"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fc9043f4fd8
我该怎么办?
答案 0 :(得分:0)
您看到的错误Missing template podcasts/create, application/create ...
是因为您未在create
操作中处理失败方案。如果@podcast.save
返回false
,那么按照惯例,它会查找与要呈现的操作具有相同名称的模板。
您可以按照以下方式将操作修改为重定向/渲染。大多数人更喜欢渲染相同的表单并显示错误。
def create
@podcast = current_user.podcasts.create(podcast_params)
if @podcast.save
redirect_to podcast_path(@podcast)
else
render :new # Change this according to your will.
end
end