Keep getting “no file selected” when attempting to

2019-09-06 02:08发布

问题:

I am trying to implement uploading images with CarrierWave by following this railscast: http://railscasts.com/episodes/253-carrierwave-file-uploads?view=comments , but whenever I submit the image it my error message just says no file selected, when I am clearly selecting a file.

I've checked over my code many, many times and I can't figure it out.

Here is my products/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 %>

Here is what is in my product model:

mount_uploader :image, ImageUploader

My new, create, and show actions from products controller:

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

defining params in products controller:

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

This is all the relevant info I can think of. Thanks for the help.

Edit: After setting multipart I get this error

syntax error, unexpected tIDENTIFIER, expecting keyword_end form_for(@product) html => { :multipart => true } do |f| ^ app/views/products/new.html.erb:3: syntax error, unexpected keyword_do_block, expecting keyword_end

I have added <% end %> tags in and I still get the same. I posted the whole view files code above.

回答1:

try with following form, your form looks messy, lot of end statements

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 %>