【问题标题】:Paperclip gem not uploading file回形针宝石不上传文件
【发布时间】:2013-12-12 20:36:05
【问题描述】:

好的,所以我已经搜索了很多关于这个问题的答案,我想我只是错过了一些明显的东西,但我是 RoR 的新手,所以请耐心等待。

我正在使用 Paperclip 并且 gem 安装良好。我一直在关注http://railscasts.com/episodes/134-paperclip?autoplay=true 上的 railscasts 教程,并且我完全按照说明进行操作,但它不起作用。

我猜这是因为不同版本的 rails 和强参数有关?我正在寻找上传食谱的图片。正如我所检查的那样,数据库迁移工作正常,并且所有相关列都存在。

包括以下所有内容:

recipe.rb 文件:

 has_attached_file :image

_recipe_form.html.erb 文件:

<%= 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 文件:

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

编辑:

添加

迁移

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

调用配方

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

【问题讨论】:

  • 您是否收到任何错误消息?在生产中还是在本地?提供一些上下文。
  • 道歉@SimoneCarletti,不,我可以填写表格,选择一张图片并提交。该表单仍然有效并添加到数据库中,但是图像文件从未上传,如果我检查数据库,图像列中没有信息。
  • 添加回形针后是否重启了服务器?
  • 是的,我试过了。你知道自 rails 4 以来,回形针的使用是否发生了变化?
  • 你能添加你的 recipe_params 方法和你的迁移吗?

标签: ruby-on-rails ruby-on-rails-4 paperclip


【解决方案1】:

感谢您的所有帮助。我设法在这方面得到了一些外部帮助。这是一个简单的修复,如果我展示了参数,我相信你会发现的!

我忘记将 :image 添加到我的参数中,如下所示

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

全部修复。谢谢!

【讨论】:

    猜你喜欢
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 2014-04-13
    • 2014-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多