Ich arbeite an einer Rails App und eine verschachtelte Ressource für Bilder. Das Modell ist Picture
, und es ist polymorph und interagiert mit ein paar verschiedenen Tabellen.Rails Büroklammer: Parameter fehlt oder der Wert ist leer
Ich arbeite an der Controller-Aktion und der create-Methode. Nach this answer und anderen Dingen, die ich mit Büroklammer gesehen habe, sollten die Parameter dem Format table_name
und dann file
folgen. Selbst wenn ich das mache, bekomme ich immer noch einen param is missing or the value is empty: picture
Fehler.
Hier ist mein Code:
Picture.rb
class Picture < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
has_attached_file :image, style: { small: '64x64', medium: '100x100', large: '200x200' }
validates_attachment :image, presence: true, content_type: { content_type: /\Aimage\/.*\Z/ },
size: { in: 0..5.megabytes }, default_url: 'missing_img.png'
acts_as_list scope: [:imageable_id, :imageable_type]
end
pictures_controller.rb
class PicturesController < ApplicationController
before_action :authorize_user!
before_action :set_resource!
def index
@pictures = @resource.pictures
end
def create
@picture = @resource.pictures.new(picture_params) do |pic|
pic.imageable_type = @resource
pic.imageable_id = @resource.id
end
if @picture.save
redirect_to :back
flash[:success] = 'Image Saved!'
else
redirect_to :back
flash[:danger] = "#{@picture.errors.full_messages.to_sentence}"
end
end
def destroy
@picture = Picture.find(params[:id])
@resource.pictures.delete(@picture)
redirect_to :back
flash[:success] = "Picture deleted"
end
private
def set_resource!
klass = [Gym, User, Location, Product].detect { |c| params["#{c.name.underscore}_id"] }
@resource = klass.find(params["#{klass.name.underscore}_id"])
end
def picture_params
params.require(:picture).permit(:image)
end
end
Bilder/index.html
<h6>Upload Pictures</h6>
<%= form_for(current_user, url: url_for(controller: 'pictures', action: 'create'), method: :post, html: { multipart: true, class: 'form-horizontal' }) do |f| %>
<%= f.file_field :image, type: :file, multiple: true, style: 'padding-bottom: 25px;' %>
<%= f.submit "Upload", class: "btn btn-gen" %>
<% end %>
Und hier ist die param Anfrage auf Formular abschicken: mit diesem
{"utf8"=>"✓",
"authenticity_token"=>"+fsA6liECF7pkUp/0BA0wDHq9Vv63jB+WBb7O/uUEDhhmIOZ22Rb1rNWDwuwPTDPNS7jg7vP/fVCVllDV21wDw==",
"user"=>{"image"=>[#<ActionDispatch::Http::UploadedFile:0x007f87b138acf8 @tempfile=#<Tempfile:/var/folders/9c/1_0mk00n297f560_fpv9jzl40000gn/T/RackMultipart20161020-25582-3qt3gc.jpg>,
@original_filename="b4lROOR.jpg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"user[image][]\"; filename=\"b4lROOR.jpg\"\r\nContent-Type: image/jpeg\r\n">]},
"commit"=>"Upload",
"user_id"=>"15"}
Auch, ich bin immer noch den Fehler. Sieht jemand etwas falsch in meinem Code, den ich beheben könnte? Jede Hilfe wird sehr geschätzt.
das ist genial und ein großartiger Refactor! Das einzige Problem, das ich habe, wenn ich dieses benutze, ist in meiner Form Ich erhalte den Fehler, dass das Bild nicht hinzugefügt worden ist. Ich werde etwas mehr debuggen und Sie wissen lassen –