0
votes

I have a Rails 4 application and just installed the Paperclip gem to handle image uploading. I can't get it working, after I've uploaded a photo it just says missing. Someone has a clue what's going wrong?

~settings/_form.html.erb

>     <%= form_for(@setting, :html => { :multipart => true }) do |f| %>
>       <% if @setting.errors.any? %>
>         <div id="error_explanation">
>           <h2><%= pluralize(@setting.errors.count, "error") %> prohibited this setting from being saved:</h2>
>     
>           <ul>
>           <% @setting.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_area :description %>
>       </div>
>       <div class="field">
>         <%= f.label :paragraph %><br>
>         <%= f.text_area :paragraph %>
>       </div>
>        <div>
>       <%= f.file_field :photo %>
>       </div>
>       <div class="actions">
>         <%= f.submit %>
>       </div>
>     <% end %>

My setting model ~setting.rb

class Setting < ActiveRecord::Base
    attr_accessible :title, :description, :paragraph

    has_attached_file :photo
end

Photo Migration

class AddAttachmentPhotoToSettings < ActiveRecord::Migration
  def self.up
    change_table :settings do |t|
      t.attachment :photo
    end
  end

  def self.down
    drop_attached_file :settings, :photo
  end
end

Setting migration

class CreateSettings < ActiveRecord::Migration
  def change
    create_table :settings do |t|
      t.string :title
      t.text :description
      t.text :paragraph

      t.timestamps
    end
  end
end

~settings/Show.html.erb

<p id="notice"><%= notice %></p>
<p> <%= image_tag @setting.photo.url %> </p> <br />
<p>
  <strong>Title:</strong>
  <%= @setting.title %>
</p>

<p>
  <strong>Description:</strong>
  <%= @setting.description %>
</p>

<p>
  <strong>Paragraph:</strong>
  <%= @setting.paragraph %>
</p>

<%= link_to 'Edit', edit_setting_path(@setting) %> |
<%= link_to 'Back', settings_path %>

Can't figure out what's wrong. The uploaded photo does'nt show it just says "Missing". Would appreciate some help! :)

2

2 Answers

1
votes

You can keep the first one : setting_params. It seems to be a method in your controller to ensure strong parameters (see: http://guides.rubyonrails.org/getting_started.html#saving-data-in-the-controller).

To resolve it, add the relation in this method like this :

private
  def setting_params
    params.require(:post).permit(:title, :description, :paragraph, :photo)
  end
0
votes

I'm glad to tell everyone that have or will have the same problem that I just figured it out!

By default your generated controller that you want to add the :photo attribute to defines "create" like this:

def create
    @setting = Setting.new(setting_params)
end

Just cange it to:

def create
    @setting = Setting.create(params[:setting])
end

(Just to be clear; Change setting to your own scaffold name.)