문제

나는 사용을 사용하여 배포 된 Rails 사이트에서 유지 보수 작업을하고 있습니다. PHOUNCE 승객. 워크 플로는 표준 3 계층 Railsian Test-Dev 생산 배열과 약간 다릅니다. 대신 병렬 Oracle 데이터베이스에 대해 실행되는 동일한 코드베이스의 두 개의 별도 설치가 있습니다. Dev 사이트는 Qa.domain.com 및 라이브 사이트 (www.domain.com)에 거주합니다.

두 가지 환경간에 다음 코드 스 니펫 ( 'vendors_controller.rb'에서 다른 동작을 경험하고 있습니다.

def create
  @user = current_user || User.new(params[:user])
  @registration = Registration.new(params[:registration])

  unless current_user

    @user.user_type = 'vendor'
    @user.active = 1

    if @user.save
      @user.activate!
      @user.has_role 'owner', @user
      @user.has_role 'vendor'
      self.current_user = user = @user

      @registration.active = 1
      @registration.email = @user.email
      @registration.user_id = @user.id
      if @registration.save
        send_confirmation(@user)
        send_solicitations_notifications(@registration) if @registration.notification_desired == true
        redirect_to thank_you_vendors_path
      else
        # THIS BEHAVIOR DIFFERS ACROSS PRODUCTION AND DEVELOPMENT
        @user.destroy
        self.current_user = user = nil
        # END DIFFERENCE
        respond_to do |format|
          format.html { render :action => 'new' }
          format.xml  { render :xml => @registration.errors, :status => :unprocessable_entity }
        end
      end

    else
      respond_to do |format|
        format.html  { render :action => 'new' }
        format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
...

주석 사이의 코드는 시스템이 해당 등록을 만들 수없는 경우 방금 만들어진 사용자 객체를 파괴합니다. 개발 서버에서는 잘 작동하지만 제작 서버에서는 제대로 작동하지 않습니다. 제작 서버에서는 사용자 객체가 등록 저장이 실패하더라도 데이터베이스 주위에 완고하게 매달려 있습니다. 생산으로 변경을 추진하는 것은 컨트롤러 파일을 업로드하고 수행하는 간단한 문제입니다. touch tmp/restart.txt 쉘을 통해. 두 코드베이스는 그렇지 않으면 동일합니다. 이 차이를 일으킬 수있는 것은 무엇입니까?

이해해 주셔서 감사합니다!

저스틴

편집 : 몇 가지 차이점이 있습니다 production.rb 문제를 진단하는 데 도움이 될 수있는 두 설치에 걸쳐 있습니다. 생산 중,

config.cache_classes = true

# Full error reports are disabled and caching is turned on
config.action_controller.consider_all_requests_local = false
config.action_controller.perform_caching             = true

개발 중에이 세 플래그는 역 값으로 설정됩니다. 감사!

도움이 되었습니까?

해결책

코드에 대해 변경해야 할 몇 가지 사항이 있습니다.

  1. 당신은 거래를 사용하지 않습니다
  2. 당신은 컨트롤러에서 너무 많이하고 있습니다

문제가 발생하는 이유는 아마도 생산과 개발의 환경 차이로 인한 것일 수 있습니다.

config.cache_classes = false

그러나 나는 당신이 당신의 모든 행동을 늦추기 때문에 생산에서 이것을 바꿔야한다고 생각하지 않습니다. 대신 생산 환경과 밀접하게 일치하는 준비 환경을 갖는 것이 좋습니다.

문제를 해결하기 위해 다음과 같은 작업을 다시 작성할 가능성이 높습니다.

# using before filters will keep your actions tight
before_filter :cannot_create_user, :if => :signed_in?

def create
  # setup all the objects
  @user = User.new(params[:user])

  @user.user_type = 'vendor'
  @user.active = 1

  @user.has_role 'owner', @user
  @user.has_role 'vendor'

  @registration = @user.registrations.build(params[:registration])
  @registration.active = 1
  @registration.email = @user.email

  # make sure everything is valid before trying to save and activate
  if @user.valid?
    @user.save!  # might not need this if activate calls save!
    @user.activate!

    # this should probably be a sign_in() method... 
    self.current_user = @user

    send_confirmation(@user)
    send_solicitations_notifications(@registration) if @registration.notification_desired?

    redirect_to thank_you_vendors_path
  else
    respond_to do |format|
      format.html { render :action => 'new' }
      format.xml  { render :xml => @registration.errors, :status => :unprocessable_entity }
    end
  end

...
end


protected 

def signed_in?
  !current_user.nil?
end

def cannot_create_user
  respond_to do |format|
    format.html  { render :action => 'new' }
    format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
  end
end

NB 나는 이것을 테스트하지 않았지만 효과가 없을 수도 있지만 아이디어를 얻어야합니다 ... 단위 테스트가 있으면 (내가 할 수 있기를 바랍니다 ...) 그것을 떨어 뜨리고 그것이 작동하는지 확인할 수 있어야합니다. !

다음 단계는 사용을 사용하는 것입니다 Accept_nested_attribute_for 등록 개체의 경우 사용자 매개 변수의 일부로 제출할 수 있습니다.

나는 또한 모든 역할 설정 등이 완료되도록 이것을 리팩토링합니다. 콜백.

이 시점에서 create 액션은 정말 간단 할 가능성이 높으며 컨트롤러를 사용하도록 전환 할 수 있습니다. 상속 자원.

이게 도움이 되길 바란다!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top