Paperclip - 无法上传图片
Paperclip - Can't upload a picture
型号
class Pin < ActiveRecord::Base
attr_accessible :description, :image
has_attached_file :image, styles: { medium: "320x240>"}
validates :description, presence: true
validates :user_id, presence: true
validates_attachment :image, presence: true,
content_type: { content_type: ["image/jpeg", "image/jpg", "image/gif", "image/png"] },
size: { in: 0..5.megabytes }
belongs_to :user
end
我选择要上传的图片,填写文本,提交,然后我收到错误消息 "This field [image] can't be blank",尽管它确实不是。问题出在哪里?
问题是您不允许访问该参数,因此
在您的控制器参数中添加 :image 参数要求,这样您就可以访问它,如下所示:
params.require(:pin).permit(YOUR PARAMETERS HERE, :image)
听起来您可能没有将您的名字列入白名单 file_field
。我不确定您使用的 Rails 是哪个版本,所以我会提供并回答 3 和 4。
在您看来:
<%= form_for @pin do |f| %>
<%= f.file_field :image %>
<% end %>
在你的控制器中:
Rails 4
def create
@pin = Pin.new(pin_params)
if @pin.save
# do your success calls here
else
# do your error calls here
end
end
private
def pin_params
# whitelist it here with "Strong Parameters" if you're using Rails 4
params.require(:pin).permit(:image)
end
如果使用 Rails 3
# ==== Pin.rb model ====
# whitelist it in your model with attr_accessible
class Pin < ActiveRecord::Base
attr_accessible :image
validates :description, presence: true
validates :user_id, presence: true
validates_attachment :image, presence: true,
content_type: { content_type: ["image/jpeg", "image/jpg", "image/gif", "image/png"] },
size: { in: 0..5.megabytes }
belongs_to :user
end
#===== PinsController.rb =======
def create
@pin = Pin.new(params[:image])
if @pin.save
# do your success calls here
else
# do your error calls here
end
end
型号
class Pin < ActiveRecord::Base
attr_accessible :description, :image
has_attached_file :image, styles: { medium: "320x240>"}
validates :description, presence: true
validates :user_id, presence: true
validates_attachment :image, presence: true,
content_type: { content_type: ["image/jpeg", "image/jpg", "image/gif", "image/png"] },
size: { in: 0..5.megabytes }
belongs_to :user
end
我选择要上传的图片,填写文本,提交,然后我收到错误消息 "This field [image] can't be blank",尽管它确实不是。问题出在哪里?
问题是您不允许访问该参数,因此 在您的控制器参数中添加 :image 参数要求,这样您就可以访问它,如下所示:
params.require(:pin).permit(YOUR PARAMETERS HERE, :image)
听起来您可能没有将您的名字列入白名单 file_field
。我不确定您使用的 Rails 是哪个版本,所以我会提供并回答 3 和 4。
在您看来:
<%= form_for @pin do |f| %>
<%= f.file_field :image %>
<% end %>
在你的控制器中:
Rails 4
def create
@pin = Pin.new(pin_params)
if @pin.save
# do your success calls here
else
# do your error calls here
end
end
private
def pin_params
# whitelist it here with "Strong Parameters" if you're using Rails 4
params.require(:pin).permit(:image)
end
如果使用 Rails 3
# ==== Pin.rb model ====
# whitelist it in your model with attr_accessible
class Pin < ActiveRecord::Base
attr_accessible :image
validates :description, presence: true
validates :user_id, presence: true
validates_attachment :image, presence: true,
content_type: { content_type: ["image/jpeg", "image/jpg", "image/gif", "image/png"] },
size: { in: 0..5.megabytes }
belongs_to :user
end
#===== PinsController.rb =======
def create
@pin = Pin.new(params[:image])
if @pin.save
# do your success calls here
else
# do your error calls here
end
end