Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails not recognizing phony_rails method #58

Closed
eastsidedev opened this issue Jun 11, 2014 · 1 comment
Closed

Rails not recognizing phony_rails method #58

eastsidedev opened this issue Jun 11, 2014 · 1 comment

Comments

@eastsidedev
Copy link

Rails 4.1
Ruby 2.0
Windows 8.1

I am using the phony_rails gem

In module models/concerns/sanitizable.rb, I have:

module Sanitizable
  extend ActiveSupport::Concern

  included do
    before_save :sanitize_phones_and_email
  end

  def sanitize_phones_and_email
    (self.email = email.downcase) if attribute_present?("email")
    (self.work_phone = phony_normalize work_phone, :default_country_code => 'US') if attribute_present?("work_phone")
    (self.mobile_phone = phony_normalize mobile_phone, :default_country_code => 'US') if attribute_present?("mobile_phone")
    (self.fax_phone = phony_normalize fax_phone, :default_country_code => 'US') if attribute_present?("fax_phone")
    (self.other_phone = phony_normalize :other_phone, :default_country_code => 'US') if attribute_present?("other_phone")
  end
end

In models/agent.rb, I have the following:

class Agent < ActiveRecord::Base
  include Sanitizable

  validates :first, presence: true
  validates :last, presence: true

end

In controllers/agents_controller, I have the following:

def new
  @agent = Agent.new
end

def create
  @agent = Agent.new(agent_params)
  respond_to do |format|
    if @agent.save
      format.html { redirect_to @agent, notice: 'Agent was successfully created.' }
      format.json { render :show, status: :created, location: @agent }
    else
      format.html { render :new }
      format.json { render json: @agent.errors, status: :unprocessable_entity }
    end
  end
end

In my helpers/agent_helper.file, I have the following method:

def create_new_agent_manually(row)
  agent = Hash.new
  agent['first'] = row[1]
  agent['last'] = row[2]
  agent['work_phone'] = row[5]
  agent['mobile_phone'] = row[6]
  agent['fax_phone'] = row[7]
  agent['other_phone'] = row[8]
  agent['email'] = row[9]
  new_agent = Agent.create(agent)
end

When create_new_agent_manually gets called, I get the following error message:

undefined method `phony_normalize' for #<Agent:0x00000006fcbdd0>      

and it provides a line number that corresponds to this line in sanitizable.rb:

(self.work_phone = phony_normalize work_phone, :default_country_code => 'US') if attribute_present?("work_phone")

The reason I have the before_save action in this separate module, is that it's used by at least three separate models and I don't want to repeat myself. In my application, I am importing data from an XLS spreadsheet (that part is working fine), and passing each row to the methods above. Any ideas?

@joost
Copy link
Owner

joost commented Jun 12, 2014

Hi, I updated you comment to properly display code.
Please read the documentation. phony_normalize is a class method. If you want to do things yourself use PhonyRails.normalize_number.

@joost joost closed this as completed Jun 12, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants