0

我在 Windows 上运行 Rails 4,并尝试自动化填充脚本。我的 populatepins.rake 文件是:

namespace :db do 
desc "Fill database with sample data"
task populatepins: :environment do
    User.all.each do |user|
        puts "[DEBUG] uploading images for user #{user.id} of #{User.last.id}"
        2.times do |n|
            image = File.open(Dir.glob(File.join(Rails.root, 'testpics', '*')).sample)  # sample picks one random
            puts "[DEBUG] searching for FILE: #{File.absolute_path(image)}"
            description = %w(cool awesome crazy wow adorable incredible).sample  # %W converts to an array
            puts "[DEBUG] past description: #{description}"
            user.pins.create!(image: image, description: description)
            puts "[DEBUG] endofloop"            
        end
    end
end
end

但是,在运行脚本时,我会收到带有此错误的循环:

C:\Sites\code\testrails>rake db:populatepins
[DEBUG] uploading images for user 45 of 47
[DEBUG] searching for FILE: C:/Sites/code/testrails/testpics/_MG_5557.jpg
[DEBUG] past description: adorable
C:/RailsInstaller/Ruby1.9.3/bin/rake: No such file or directory - lsof -p 13752
C:/RailsInstaller/Ruby1.9.3/bin/rake: No such file or directory - lsof -p 13752
C:/RailsInstaller/Ruby1.9.3/bin/rake: No such file or directory - lsof -p 13752
C:/RailsInstaller/Ruby1.9.3/bin/rake: No such file or directory - lsof -p 13752
[DEBUG] endofloop

我认为这可能是https://www.ruby-forum.com/topic/194682中的 '\' vs '/' 路径问题,但无法真正找到可行的解决方案......

任何帮助将不胜感激,谢谢!

编辑:这是模型的样子(JLX 是我在评论...)

class Pin < ActiveRecord::Base
has_attached_file :image, styles: { large: "640x480>", medium: "320x240>", thumb: "200x200#"} # Paperclip association

validates :description, presence: true  #, length: { minimum: 10 }
validates :user_id, presence: true
validates_attachment :image, presence: true,
                             content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif']},
                             size: {less_than: 5.megabytes}

# JLX link to users
belongs_to :user  # can only have one user associated

# We are modifying the default getter/setter method created automatically when generating the image_url var
def image_remote_url=(url_value)
    self.image = URI.parse(url_value) unless url_value.blank?
    super  # inherits the default method behaviour
end
end

这就是控制器的样子:

class PinsController < ApplicationController
    # JLX Devise method
    before_action :authenticate_user!, except: [:index, :show]
    before_action :set_pin_global, only: [:show]
    before_action :set_pin_user, only: [:edit, :update, :destroy]

    # GET /pins
    # GET /pins.json
    def index
    #@pins = Pin.all
    @pins = Pin.order("created_at desc")
    end

    # GET /pins/1
    # GET /pins/1.json
    def show
    end

    # GET /pins/new
    def new
    #@pin = Pin.new
    @pin = current_user.pins.new
    end

    # GET /pins/1/edit
    def edit
    end

    # POST /pins
    # POST /pins.json
    def create
    #@pin = Pin.new(pin_params)
    @pin = current_user.pins.new(pin_params)

    respond_to do |format|
        if @pin.save
            format.html { redirect_to @pin, notice: 'Pin was successfully created.' }
            format.json { render action: 'show', status: :created, location: @pin }
        else
            format.html { render action: 'new' }
            format.json { render json: @pin.errors, status: :unprocessable_entity }
        end
    end
    end

    # PATCH/PUT /pins/1
    # PATCH/PUT /pins/1.json
    def update
        respond_to do |format|
            if @pin.update(pin_params)
                format.html { redirect_to @pin, notice: 'Pin was successfully updated.' }
                format.json { head :no_content }
            else
                format.html { render action: 'edit' }
                format.json { render json: @pin.errors, status: :unprocessable_entity }
            end
        end
    end

    # DELETE /pins/1
    # DELETE /pins/1.json
    def destroy
        @pin.destroy
        respond_to do |format|
            format.html { redirect_to pins_url }
            format.json { head :no_content }
        end
    end

    private
    # Use callbacks to share common setup or constraints between actions.
    def set_pin_global
        @pin = Pin.find(params[:id])
    end

    def set_pin_user
        @pin = current_user.pins.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def pin_params
    params.require(:pin).permit(:description, :image, :image_remote_url)  # JLX added :image
    end
end
4

0 回答 0