简体   繁体   中英

Paperclip gem not uploading file

Ok so I have searched around a lot for an answer to this and I think I am just missing something obvious but I'm new to RoR so bear with me.

I'm using Paperclip and the gem installed fine. I have been following the tutorial on railscasts at http://railscasts.com/episodes/134-paperclip?autoplay=true and I have followed the instructions perfectly but it doesn't work.

I'm guessing it is because of a different version of rails and something to do with strong parameters? I'm looking to upload a picture for a recipe upload. The database migrations have worked as I have checked and all relevant columns are present.

All of the below are included:

recipe.rb file:

 has_attached_file :image

_recipe_form.html.erb file:

<%= form_for(@recipe, :html => { :multipart => true }) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_field :name, placeholder: "My Recipe" %>
  </div>
  <div class="field">
    <%= f.text_area :description, placeholder: "Write a description..." %>
  </div>
  <div class="field">
    <%= f.file_field :image %>
  </div>
  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

recipe_controller.rb file:

def create
  @recipe = current_user.recipes.build(recipe_params)
  if @recipe.save
    flash[:success] = "Recipe Created!"
    redirect_to @recipe
  else
    @feed_items = []
    render 'new'
  end
end

edit:

Added

Migrations

class AddAttachmentImageToRecipes < ActiveRecord::Migration
  def self.up
    change_table :recipes do |t|
      t.attachment :image
    end
  end

  def self.down
    drop_attached_file :recipes, :image
  end
end

Call to recipe

@recipe = Recipe.find(params[:id])

Thank you for all your help. I managed to get some outside help on this. It was a simple fix and one I am sure you would have spotted if I had shown the params!

I forgot to add in :image to my params as below

params.require(:recipe).permit(:name, :description, :live, :image)

All fixed. thank you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM