Model method called in controller returning "undefined method"

95 Views Asked by At

Working on some legacy code and essentially copying/pasting some functionality, but getting an error that isn't making sense to me.

In my controller I have a CASE statement setup that checks the for the current page a user is on, and determines if a text field needs to be checked by calling the model method valid_in_state?(). In the CASE statement, the method is called just fine when using it for the symbol :name. If I try to mimic that same behavior for :address I get an error returned with:

undefined method `valid_in_state?' for true:TrueClass

This doesn't make sense to me since I know the method is defined and available within the model and works for :name.

Here's the code shortened to what's relevant.

Model:

class Response < ActiveRecord::Base

  # validation for names
  validates_presence_of :namelast,
                        :namefirst,
                        :if => :name_state?
  validates_length_of   :namelast,
                        :maximum  => 50,
                        :allow_blank => true,
                        :message => RESPONSE_TEXT['validation']['name_length'],
                        :if => :name_state?
  validates_length_of   :namefirst,
                        :maximum  => 50,
                        :allow_blank => true,
                        :message => RESPONSE_TEXT['validation']['name_length'],
                        :if => :name_state?
  validates_length_of   :namemiddle,
                        :maximum  => 50,
                        :allow_blank => true,
                        :message => RESPONSE_TEXT['validation']['name_length'],
                        :if => :name_state?
  validates_length_of   :namesuffix,
                        :maximum  => 4,
                        :allow_blank => true,
                        :message => RESPONSE_TEXT['validation']['name_length'],
                        :if => :name_state?

                        
  validates_presence_of :home_street,
                        :if => :address_state?


  def valid_in_state?(state)
    @state = state
    valid?
  end

  def state?(state)
    self.send("#{state}_state?")
  end
 

  private
    def name_state?
      @state == :name
    end
    def address_state?
      @state == :address
    end
end

Controller:

def update

    summons = get_summons(@current_person)

    if(params[:response])
      @response = Response.find(summons.summonsid)
      @response.attributes = params[:response]


    case UI_TEXT['buttons']['next']
    when params[:intro]            then
      return if correct_position_state_from_post?(:questionnaire, :intro)
      set_position_state(:questionnaire, :name)
      redirect_to name_questionnaire_url
    when params[:name]             then
      return if correct_position_state_from_post?(:questionnaire, :name)
      if @response.valid_in_state?(:name)
        @response.save
        if session[:return_to_contact]
          set_position_state(:questionnaire, :contact)
          redirect_to session[:return_to_contact]
          session[:return_to_contact] = nil
        else
          set_position_state(:questionnaire, :address)
          redirect_to address_questionnaire_url
        end
      else
        render :action=> "name"
      end
    when params[:address]          then
      return if correct_position_state_from_post?(:questionnaire, :address)
      #address validate
      if @response.valid_in_state?(:address)
        @response.save
      end
    end
end

My ruby on rails knowledge is limited, but to me this should work. Or at least the method should be recognized since it literally works for the :name area of the CASE statement.

0

There are 0 best solutions below