dimanche 30 avril 2017

image uploading error ruby on rails

I am getting the two errors, shown in the image below, when I try to upload an image using carrierwave. The image field goes blank every time I try to upload an image. I am new to ruby on rails and am looking for anything that will help.

enter image description here

post_controller.rb

  # GET /posts/new
def new
  @post = Post.new
end

# POST /posts
# POST /posts.json
def create
  @post = Post.new(post_params)

respond_to do |format|
  if @post.save
    format.html { redirect_to @post, notice: 'Post was successfully created.' }
    format.json { render :show, status: :created, location: @post }
  else
    format.html { render :new }
    format.json { render json: @post.errors, status: :unprocessable_entity }
  end
 end
end

create_posts.rb (the model file)

class CreatePosts < ActiveRecord::Migration[5.0]
  def change
    create_table :posts do |t|
      t.string :title
      t.string :image
      t.text :description

      t.timestamps null:false
   end
 end
end

_form.html.erb (the view file)

<%= form_for(post) do |f| %>
  <% if post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(post.errors.count, "error") %> prohibited this post from 
    being saved:</h2>

  <ul>
  <% post.errors.full_messages.each do |message| %>
    <li><%= message %></li>
  <% end %>
  </ul>
 </div>
<% end %>

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

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

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

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

Aucun commentaire:

Enregistrer un commentaire