仅在设计给出的after_sign_in_path_for中创建一次方法init

时间:2014-07-22 17:54:03

标签: ruby-on-rails ruby devise salesforce

更新更新

如果我拉出salesforce方法并在我的应用程序控制器中触发它,它就可以正常工作。它由设计给出的after_sign_in_path_for方法触发。这轮谈判没有奏效,因为似乎从salesforce返回的响应失败了因为它被送了两次。我猜这个方法在刷新页面时被击中了两次?但是我必须在更新后初始化该方法,这是下一页的位置,现在是最后一页。任何想法只能使它初始化一次?

class ApplicationController < ActionController::Base
  before_filter :configure_permitted_parameters, if: :devise_controller?

protect_from_forgery

  def salesforce_add_contact
    client = Databasedotcom::Client.new("config/databasedotcom.yml")
    client.authenticate(:username => "george@thirdmind.com.admsbox", :password => "newD3v13!DotcZyckaLNki3jwKCEfoHfdy" )

    #ruby syntax?
    params = { :PortalID => current_user.id.to_s,
               :firstname => current_user.first_name,
               :lastname => current_user.last_name,
               :email => current_user.email,
               :country_of_residence => current_user.country_of_residence,
               :primary_language => current_user.primary_language,
               :high_school_graduation_year => current_user.high_school_graduation_year}

    params = ActiveSupport::JSON.encode(params)
    path = "/services/apexrest/v2/portalAccount"
    result = client.http_post(path, params)
    result = ActiveSupport::JSON.decode(result.body)

    # if (result['status'] == "Success")
    #   self.sfdc_contact_id = result['ContactId']
    # end
  end

  def not_found(msg="Not Found")
    raise ActionController::RoutingError.new(msg)
  end

  def after_sign_in_path_for(resource_or_scope)
    chooseapplication_path
    #salesforce_add_contact()
  end 

  protected
  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << :first_name << :last_name << :high_school_graduation_year
    devise_parameter_sanitizer.for(:account_update) << :high_school_graduation_year << :country_of_residence << :primary_language << :password << :password_confirmation
  end
end

更新

也许不允许检索这些值?我正在使用设计,但我不认为配置允许的参数适用于salesforce方法(我认为登录,注册,更新工作).....还有一些值确实在salesforce方法中检索到了不是那些得到更新的。

class ApplicationController < ActionController::Base
  before_filter :configure_permitted_parameters, if: :devise_controller?

#Catch all CanCan errors and alert the user of the exception
  rescue_from CanCan::AccessDenied do | exception |
    redirect_to root_url, alert: exception.message 
  end

protect_from_forgery

  def not_found(msg="Not Found")
    raise ActionController::RoutingError.new(msg)
  end

  def after_sign_in_path_for(resource_or_scope)
    page_path('chooseapplication')
  end 

  def after_sign_out_path_for(resource_or_scope)
    root_url
  end

  protected
  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << :first_name << :last_name << :high_school_graduation_year << :role << :uid << :provider
    devise_parameter_sanitizer.for(:sign_in) << :first_name << :last_name << :high_school_graduation_year << :role << :uid << :provider << :email << :password
    devise_parameter_sanitizer.for(:account_update) << :high_school_graduation_year << :country_of_residence << :primary_language << :password << :password_confirmation
  end
end

我有一个注册控制器,它继承了设计注册。

class RegistrationsController < Devise::RegistrationsController
  #after_action :salesforce_add_contact

  def update
    account_update_params = devise_parameter_sanitizer.sanitize(:account_update)

    # required for settings form to submit when password is left blank
    if account_update_params[:password].blank?
      puts "entered if statement"
      account_update_params.delete("password")
      account_update_params.delete("password_confirmation")
      puts account_update_params.inspect
    end

    @user = User.find(current_user.id)
    if @user.update_attributes(account_update_params)
      set_flash_message :notice, :updated
      # Sign in the user bypassing validation in case their password changed
      sign_in @user, :bypass => true
      redirect_to after_update_path_for(@user)
      #salesforce_add_contact()
    else
      render "edit"
    end
  end

  protected

  def after_sign_up_path_for(resource) 
    page_path('verifyemail')
  end 

  def after_inactive_sign_up_path_for(resource)
    page_path('verifyemail')
  end 

  def salesforce_add_contact
    client = Databasedotcom::Client.new("config/databasedotcom.yml")
    client.authenticate(:username => "secret", :password => "secret" )

    params = { PortalID: current_user.id.to_s,
               firstname: current_user.first_name,
               lastname: current_user.last_name,
               email: current_user.email,
               HS_Grad_Year: current_user.high_school_graduation_year.to_s}
               #country_of_residence: self.country_of_residence,
               #primary_language: self.primary_language }

    params = ActiveSupport::JSON.encode(params)
    path = "/services/apexrest/v1/portalAccount"
    result = client.http_post(path, params)
    result = ActiveSupport::JSON.decode(result.body)

    # if (result['status'] == "Success")
    #   self.sfdc_contact_id = result['ContactId']
    # end
  end
end

update方法将users表的三个新值发布到当前用户的记录中。

salesforce方法假设检索这些值,然后通过databasedotcom gem将这些值推送到salesforce。这意味着salesforce方法应该在更新方法之后。我无法实现这一点。

我尝试将该方法放在语句末尾的更新中。我尝试在控制器中放置一个after_action逻辑。我尝试了一些令人讨厌的黑客而且它不起作用。

0 个答案:

没有答案