paperclip 4 and ruby on rails 4 problems -
i'm learning rails4 , want create album management app
i choose paperclip this.
but failed upload image , me?
photo controller - upload method receive post upload_test method
class photoscontroller < applicationcontroller before_action :set_photo, only: [:show, :edit, :update, :destroy] # /photos # /photos.json def index @photos = photo.all end # /photos/1 # /photos/1.json def show end # /photos/new def new @photo = photo.create end # /photos/1/edit def edit end def upload @photo = photo.create(photo_params) @photo.album_id = params[:album_id] @photo.avatar = params[:avatar] if @photo.save render text: "1" else render text: "0" end end def upload_test @photo = photo.create end # post /photos # post /photos.json def create @photo = photo.create(photo_params) respond_to |format| if @photo.save format.html { redirect_to @photo, notice: 'photo created.' } format.json { render action: 'show', status: :created, location: @photo } else format.html { render action: 'new' } format.json { render json: @photo.errors, status: :unprocessable_entity } end end end # patch/put /photos/1 # patch/put /photos/1.json def update respond_to |format| if @photo.create(photo_params) format.html { redirect_to @photo, notice: 'photo updated.' } format.json { head :no_content } else format.html { render action: 'edit' } format.json { render json: @photo.errors, status: :unprocessable_entity } end end end # delete /photos/1 # delete /photos/1.json def destroy @photo.destroy respond_to |format| format.html { redirect_to photos_url } format.json { head :no_content } end end private # use callbacks share common setup or constraints between actions. def set_photo @photo = photo.find(params[:id]) end # never trust parameters scary internet, allow white list through. def photo_params #params.permit(:avatar) end end
view : upload_test.html.erb
<%= form_tag("/photos/upload", method: "post" , multipart: true) %> <%= text_field_tag :album_id %> <%= file_field_tag :avatar %> <%= submit_tag("save") %> <% end %>
model :
class photo < activerecord::base has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png" validates_attachment_content_type :avatar, :content_type => /\aimage\/.*\z/ end
and development.rb
h
air::application.configure # settings specified here take precedence on in config/application.rb. # in development environment application's code reloaded on # every request. slows down response time perfect development # since don't have restart web server when make code changes. config.cache_classes = false # not eager load code on boot. config.eager_load = false # show full error reports , disable caching. config.consider_all_requests_local = true config.action_controller.perform_caching = false # don't care if mailer can't send. config.action_mailer.raise_delivery_errors = false # print deprecation notices rails logger. config.active_support.deprecation = :log # raise error on page load if there pending migrations config.active_record.migration_error = :page_load # debug mode disables concatenation , preprocessing of assets. # option may cause significant delays in view rendering large # number of complex assets. config.assets.debug = true paperclip.options[:command_path] = 'c:\program files\imagemagick-6.8.8-q8' end
post params got :
parameters: {"utf8"=>"✓", "authenticity_token"=>"02rmntojfa92jh1iho9umupyhxnyg99m8r8scky9nhy=", "album_id"=>"1", "avatar"=>#<actiondispatch::http::uploadedfile:0x228fdc8 @tempfile=#<file:c:/users/casper~1.hua/appdata/local/temp/rackmultipart20140206-8076-1hqif1h>, @original_filename="_mg_0059.jpg", @content_type="image/jpeg", @headers="content-disposition: form-data; name=\"avatar\"; filename=\"_mg_0059.jpg\"\r\ncontent-type: image/jpeg\r\n">, "commit"=>"save"}
you'll need amend these areas:
strong params
#app/controllers/photos_controller.rb def photo_params params.require(:photo).permit(:avatar) end
new / create actions
#app/controllers/photos_controller.rb def new @photo = photo.new end def new @photo = photo.new(photo_params) if @photo.save render text: "1" else render text: "0" end end private def photo_params params.require(:photo).permit(:avatar) end
Comments
Post a Comment