3

im following ryan bates screen cast on how http://railscasts.com/episodes/219-active-model on how to validate a form without a database

but i keep getting an undefined method valid?

heres my controller

def create
  @contacts = FreshDeskApiWrapper.new().post_tickets(params[:contacts])
  if @contacts.valid?
    redirect_to new_contact_path 
  else
   flash[:notice] = "OOps"
   render action: 'new'
 end

end

I can seem to call

 $ FreshDeskApiWrapper.new().valid?

just fine in the console but it does not seem to like it when i tack on the

 $ FreshDeskApiWrapper.new().post_tickets(params[email: '[email protected]']).valid?

i get an undefined method valid?

There is something im not understanding about this

heres my fresh_desk_api_wrapper.rb file i created in my models folder

  class FreshDeskApiWrapper
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor :config, :client, :subject, :email, :custom_field_phone_number_50754,      :custom_field_company_50754, :description
  validates :subject, presence: true   
  validates :email, presence: true  
  validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i

  def initialize(attributes = {})
    attributes.each do |name, value|
    send("#{name}=", value)
  end
  self.config = YAML.load_file("#{Rails.root}/config/fresh_desk.yml")[Rails.env]
  self.client = Freshdesk.new(config[:url], config[:api_key], config[:password])
  end

  def post_tickets(params)
    client.post_tickets(params)
  end

  def persisted?
    false
  end
end

post_tickets is something im defining in there

6
  • what does it say that it is undefined for i get an undefined method valid? , this an activerecord instance method , i am pretty sure the return type of def post_tickets(params) client.post_tickets(params) end is not aciverecord instance Commented Aug 11, 2013 at 3:22
  • Please show the precise error message and stack trace. Hint: post_tickets doesn't return anything with a valid? method. Commented Aug 11, 2013 at 3:25
  • @DaveNewton - heres the entire error message undefined method `valid?' for #<String:0x007fd4ac3a2338> Commented Aug 11, 2013 at 3:35
  • @user1502223 Right, because post_tickets doesn't return anything with a valid? method. If you're trying to call valid? on an AR instance, you can't just tack it on to arbitrary methods that return other types (like a String). Commented Aug 11, 2013 at 3:37
  • @DaveNewton = ic thank you for the clearing that up....so i guess i need to find another way to validate this form? Commented Aug 11, 2013 at 3:39

3 Answers 3

4

You can call valid? on an single instance of an object, not multiple. @contacts would imply that your post_tickets method is returning multiple objects.

Sign up to request clarification or add additional context in comments.

Comments

2

try something like this:

@contacts = FreshDeskApiWrapper.new(post_tickets(params[:contacts])

what seems to be the problem is that the method you are adding dosnt return a active record object, so the method valid? is not available

Edit:

maybe this:

@contacts = FreshDeskApiWrapper.new(FreshDeskApiWrapper.post_tickets(params[:contacts])

1 Comment

thanks for the suggestion i tried what you said but got a undefined method `post_tickets' for #<Website::ContactsController:0x007fd4ac3691a0>
0

omg im so dumb so what i did was

  def create

  @contacts = FreshDeskApiWrapper.new(params[:contacts])
  @contacts.post_tickets(params[:contacts])
  if @contacts.valid?
    redirect_to new_contact_path 
  else
   flash[:notice] = "OOps"
   render action: 'new'
  end
end

and it works!

Im still struggling to learn all this.....thanks for your guy's guidance it really helped

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.