Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CarrierWave File Uploads not working in rails

I need to upload image to my filme collection application i use carrierwave to do this (follows the railscasts steps)

step 1 i add gem 'carrierwave', '~> 0.9' to my Gemfile then run bundle step 2 rails g uploader image then rails g scaffold filmes name moviestype after rake db step 3 rails g migration add_image_to_filmes image:string and then rake db

other step are same as railscasts

in my filme modle

attr_accessible :name, :moviestype, :image
  mount_uploader :image, ImageUploader

in my _form.html.erb

<%= form_for @filme, :html => {:multipart => true} do |f| %>
  <% if @filme.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@filme.errors.count, "error") %> prohibited this filme from being saved:</h2>

      <ul>
      <% @filme.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :moviestype %><br>
    <%= f.text_field :moviestype %>
  </div>
  <div class="field">
   <br>
    <%= f.file_field :image %>
  </div>

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

in show.html.erb

<p id="notice"><%= notice %></p>

<p>
  <strong>Name:</strong>
  <%= @filme.name %>
</p>

<p>
  <strong>Moviestype:</strong>
  <%= @filme.moviestype %>
</p>
<%= image_tag @filme.image_url(:thumb) if @filme.image? %>

<%= link_to 'Edit', edit_filme_path(@filme) %> |
<%= link_to 'Back', filmes_path %>

problem is it didn't upload any image but film name and other fields are inserting to db.how can i fix this ?need quick help.

like image 792
Nuwan Indika Avatar asked Oct 01 '22 02:10

Nuwan Indika


1 Answers

As per the error logs, you have not permitted image to be saved in the database. In your FilmesController, you need to permit :image as

def filme_params
  params.require(:filme).permit(:name, :moviestype, :image) ## Add :image attribute
end
like image 163
Kirti Thorat Avatar answered Oct 13 '22 10:10

Kirti Thorat