Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby on rails - undefined method valid?

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

like image 886
user1502223 Avatar asked Mar 13 '26 16:03

user1502223


2 Answers

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.

like image 71
sevenseacat Avatar answered Mar 16 '26 08:03

sevenseacat


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])

like image 31
Rodrigo Zurek Avatar answered Mar 16 '26 07:03

Rodrigo Zurek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!