【问题标题】:Keep getting "no file selected" when attempting to upload an image using carrier wave?尝试使用载波上传图像时不断收到“未选择文件”?
【发布时间】:2014-09-12 15:14:44
【问题描述】:

我正在尝试通过遵循此 railscast:http://railscasts.com/episodes/253-carrierwave-file-uploads?view=comments 来实现使用 CarrierWave 上传图像,但是每当我提交图像时,当我明确选择文件时,我的错误消息只会显示未选择文件。

我已经检查了很多次我的代码,但我无法弄清楚。

这是我的产品/new.html.erb

<h1> new product </h1>
<% @product = Product.new %>
<% form_for(@product) html => { :multipart => true } do |f| %>
<% if @product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@product.errors.count, "error") %>
    prohibited this post from being saved:</h2>
 <% end %>
<% end %>
    <ul>
  <% @product.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
  </div>
  <% end %>

   <div class="field">
   <%= f.label :title %><br />
   <%= f.text_field :title %>
   </div>

  <div class="field">
   <%= f.label :description %><br />
   <%= f.text_field :description %>
  </div>

 <div class="field">
  <%= f.file_field :image %>

  <div class="field">
    <%= f.label :remote_image_url, "or image url" %><br />
    <%= f.text_field :remote_image_url %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
 <% end %>

这是我的产品模型中的内容:

mount_uploader :image, ImageUploader

我在产品控制器中的新建、创建和显示操作:

def new
@product = Product.new(params[:id])
end

def create
@product = Product.new(product_params)

if @product.save
    redirect_to @product
else
    render :new
end
end

def show
@product = Product.find(params[:id])
end

在产品控制器中定义参数:

private
def product_params
params.require(:product).permit(:title, :description, :image, :remote_image_url)
end 
end

这是我能想到的所有相关信息。感谢您的帮助。

编辑:设置多部分后出现此错误

语法错误,意外的 tIDENTIFIER,期待 keyword_end form_for(@product) html => { :multipart => true } do |f| ^ app/views/products/new.html.erb:3:语法错误,意外的keyword_do_block,期待keyword_end

我已经添加了 标签,我仍然得到相同的结果。我在上面发布了整个视图文件代码。

【问题讨论】:

  • 您的代码中有些东西似乎有点不对劲(可能是故意但只是检查):(1)新方法不会从 url coz 获取 id 输入 .. 它是新的 .. 尚未出生的对象(2) 通常但不一定是形式视图存在于局部
  • 好吧,我不认为改变其中任何一个可以解决我的问题。
  • 您的表单是多部分的吗?您没有发布完整的表格,所以我无法确定,但对于文件上传,您必须设置 multipart: true
  • 正如@engineersmnky 所说,您似乎忘记在表单中设置多部分句子
  • 我尝试设置多部分,但出现错误..我编辑了问题以显示整个当前视图

标签: ruby-on-rails ruby ruby-on-rails-4 carrierwave


【解决方案1】:

试试下面的表格,你的表格看起来很乱,结尾语句很多

products_controller.rb

  def new
     @product = Product.new
  end

products/new.html.erb

<h1> new product </h1>
<%= form_for @product do |f| %>
    <% if @product.errors.any? %>
      <div id="error_explanation">
        <h2><%= pluralize(@product.errors.count, "error") %>
                prohibited this post from being saved:</h2>
        <ul>
          <% @product.errors.full_messages.each do |msg| %>
             <li><%= msg %></li>
          <% end %>
        </ul>
      </div>
    <% end %>

   <div class="field">
      <%= f.label :title %><br />
      <%= f.text_field :title %>
   </div>

   <div class="field">
      <%= f.label :description %><br />
      <%= f.text_field :description %>
    </div>

   <div class="field">
       <%= f.file_field :image %>
   </div>

   <div class="field">
       <%= f.label :remote_image_url, "or image url" %><br />
       <%= f.text_field :remote_image_url %>
   </div>

   <div class="actions">
       <%= f.submit %>
   </div>
 <% end %>

【讨论】:

  • 我需要它是一个多部分表单
  • 如果您使用的是载波,则无需添加。我已经完成了数千次而没有添加 enctype。
猜你喜欢
  • 1970-01-01
  • 2021-01-10
  • 2019-10-16
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
  • 2023-02-24
  • 2015-06-08
  • 2017-06-30
相关资源
最近更新 更多