0

私は最近、ファイルをアップロードしているように見えるRoRプロジェクトにインポートオプションを追加し、このファイルをdbに正常に保存しましたが、新しいユーザーを作成して保存しようとすると、次のエラーが発生します

Started GET "/imports/13/proc" for 127.0.0.1 at 2012-07-11 02:52:00 +1000
Processing by ImportsController#proc_csv as HTML
  Parameters: {"id"=>"13"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  Import Load (0.1ms)  SELECT "imports".* FROM "imports" WHERE "imports"."id" = ? LIMIT 1  [["id", "13"]]
   (0.1ms)  begin transaction
  User Exists (0.2ms)  SELECT 1 FROM "users" WHERE "users"."email" = 'paul.mcguane1@westnet.com.au' LIMIT 1
   (0.1ms)  rollback transaction
   (0.1ms)  begin transaction
  User Exists (0.2ms)  SELECT 1 FROM "users" WHERE "users"."email" = 'compact721@***.com' LIMIT 1
   (0.1ms)  rollback transaction
   (0.1ms)  begin transaction
  User Exists (0.2ms)  SELECT 1 FROM "users" WHERE "users"."email" = 'paul.****@me.com' LIMIT 1
   (0.1ms)  rollback transaction
   (0.1ms)  begin transaction
  User Exists (0.2ms)  SELECT 1 FROM "users" WHERE "users"."email" = 'duncansingleton@****.com' LIMIT 1
   (0.1ms)  rollback transaction
   (0.1ms)  begin transaction
   (0.4ms)  UPDATE "imports" SET "processed" = 4, "updated_at" = '2012-07-10 16:52:01.146297' WHERE "imports"."id" = 13
[paperclip] Saving attachments.
   (3.5ms)  commit transaction

これは、インポートコントローラーにあるコードです

def new_user(line)
    params = Hash.new
    params[:user] = Hash.new
    params[:user]["email"] = line[0]
    params[:user]["gender"] = "male"
    params[:user]["password"] = "abc231"
    params[:user]["first_name"] = line[1]
    params[:user]["last_name"] = line[3]
    user = User.new(params[:user])
    user.save
  end


def proc_csv
    @import = Import.find(params[:id])
    lines = parse_csv_file(@import.csv.path)
    lines.shift #comment this line out if your CSV file doesn't contain a header row
    if lines.size > 0
      @import.processed = lines.size
      lines.each do |line|
        case @import.datatype
          when "releases"
            new_user(line)
            flash[:notice] = "Saving data processing was successful."
        end
      end
      @import.save
      flash[:notice] = "CSV data processing was successful."
      redirect_to :action => "show", :id => @import.id
    else
      flash[:error] = "CSV data processing failed."
      render :action => "show", :id => @import.id
    end
  end

  private

  def parse_csv_file(path_to_csv)
    lines = []

    #if not installed run, sudo gem install fastercsv
    #http://fastercsv.rubyforge.org/
    require 'csv'

    CSV.foreach(path_to_csv) do |row|
      lines << row
    end
    lines
  end

ユーザーモデル

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :admin, :manager
  attr_accessible :provider, :uid
  # attr_accessible :title, :body

  belongs_to :team
  has_many :availabilities
  attr_accessible :last_name, :first_name, :jersey, :dob, :gender, :position, :team_id, :mobile, :state, :city, :postcode



  GENDER_TYPES = [:Male, :Female]
  STATE_TYPES = [:ACT, :VIC, :QLD, :NSW, :TAS, :NT, :WA, :SA]

  validates :first_name, :last_name, :gender, :presence => true
  validates :email, :presence => true, :uniqueness => true
  validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => "isn't a valid email address"
  validates_format_of :mobile, :with => /^(?:\+?61|0)4(?:[01]\d{3}|(?:2[1-9]|3[0-57-9]|4[7-9]|5[0-35-9]|6[679]|7[078]|8[178]|9[7-9])\d{2}|(?:20[2-9]|444|68[3-9]|79[01]|820|901)\d|(?:200[01]|2010|8984))\d{4}$/, :message => "isn't a valid mobile number", :allow_blank => true
  validates_format_of :postcode, :with => /^[0-9]{4}/, :message => "isn't a valid Australian postcode", :allow_blank => true

レールコンソールメッセージ

1.9.3p194 :001 > params = {
1.9.3p194 :002 >                :user => {
1.9.3p194 :003 >                          :gender => "male",
1.9.3p194 :004 >                       :last_name => "paul",
1.9.3p194 :005 >                        :password => "abc231",
1.9.3p194 :006 >                      :first_name => "mcguane",
1.9.3p194 :007 >                           :email => "paul.mcguane1@westnet.com.au"
1.9.3p194 :008?>                }
1.9.3p194 :009?>            }
 => {:user=>{:gender=>"male", :last_name=>"paul", :password=>"abc231", :first_name=>"mcguane", :email=>"paul.mcguane1@westnet.com.au"}} 
1.9.3p194 :010 > user = User.new(params[:user])
 => #<User id: nil, postcode: nil, city: nil, state: nil, manager: nil, admin: nil, mobile: nil, team_id: nil, position: nil, gender: "male", dob: nil, email: "paul.mcguane1@westnet.com.au", jersey: nil, last_name: "paul", first_name: "mcguane", created_at: nil, updated_at: nil, encrypted_password: "$2a$10$i81k9gX3uYJ95oID.deGyOtDGFSnK6ooI0BIUiP6UtgZ...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: nil, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, provider: nil, uid: nil> 
1.9.3p194 :011 > user.valid?
  User Exists (0.2ms)  SELECT 1 FROM "users" WHERE "users"."email" = 'paul.mcguane1@westnet.com.au' LIMIT 1
  User Exists (0.1ms)  SELECT 1 FROM "users" WHERE "users"."email" = 'paul.mcguane1@westnet.com.au' LIMIT 1
 => true 
1.9.3p194 :012 > user.errors.full_messages
 => [] 
1.9.3p194 :013 >

Railsコンソールで!方法

    1.9.3p194 :013 > user.save!
   (0.1ms)  SAVEPOINT active_record_1
  User Exists (0.3ms)  SELECT 1 FROM "users" WHERE "users"."email" = 'paul.mcguane1@westnet.com.au' LIMIT 1
  User Exists (0.2ms)  SELECT 1 FROM "users" WHERE "users"."email" = 'paul.mcguane1@westnet.com.au' LIMIT 1
  SQL (135.5ms)  INSERT INTO "users" ("admin", "city", "created_at", "current_sign_in_at", "current_sign_in_ip", "dob", "email", "encrypted_password", "first_name", "gender", "jersey", "last_name", "last_sign_in_at", "last_sign_in_ip", "manager", "mobile", "position", "postcode", "provider", "remember_created_at", "reset_password_sent_at", "reset_password_token", "sign_in_count", "state", "team_id", "uid", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["admin", nil], ["city", nil], ["created_at", Wed, 11 Jul 2012 20:05:28 EST +10:00], ["current_sign_in_at", nil], ["current_sign_in_ip", nil], ["dob", nil], ["email", "paul.mcguane1@westnet.com.au"], ["encrypted_password", "$2a$10$i81k9gX3uYJ95oID.deGyOtDGFSnK6ooI0BIUiP6UtgZOuzWzQbFy"], ["first_name", "mcguane"], ["gender", "male"], ["jersey", nil], ["last_name", "paul"], ["last_sign_in_at", nil], ["last_sign_in_ip", nil], ["manager", nil], ["mobile", nil], ["position", nil], ["postcode", nil], ["provider", nil], ["remember_created_at", nil], ["reset_password_sent_at", nil], ["reset_password_token", nil], ["sign_in_count", nil], ["state", nil], ["team_id", nil], ["uid", nil], ["updated_at", Wed, 11 Jul 2012 20:05:28 EST +10:00]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
 => true 
4

1 に答える 1

1

コード スニペットとログ出力から、検証の問題が原因でユーザーが保存されていないようです。通常、このような問題が発生した場合は、Rails コンソールにドロップして、コードが実行しようとしていることがコンソールでも機能するかどうかを確認します。コンソールで動作する場合、コードは動作するはずです。

これを試して

$ rails c --sandbox

> params = {
           :user => {
                   :gender => "male",
                :last_name => "paul",
                 :password => "abc231",
               :first_name => "mcguane",
                    :email => "paul.mcguane1@westnet.com.au"
           }
         }

> user = User.new(params[:user])
> user.valid?
> user.errors.full_messages

そうすれば、本当にユーザー検証が問題の原因であるかどうかを特定できます。それはまったく別のものかもしれません。ユーザーモデルコードを投稿すると、それも役立ちます。

linesまた、イテレータ内でフラッシュ通知を設定していることにも気付きました。行を反復するたびに上書きするだけです。

于 2012-07-11T08:45:50.313 に答える