0

I receive a noMethodError when attempting to utilize a method in my application_helper file.

It looks like the helper method itself is fine, but the code inside the helper method is whats triggering the error. I have double checked my model and schema files to be certain that I had the attributes listed correctly. A fresh pair of eyes on this would be well received.

Here's my code...

application_helper.rb

PS. I'm showing both sets of helper methods here because this first set (Resident Helper Methods) works fine in the views it was intended for.

    # START -- Resident Helper Methods
      def resident_full_name
        ([@resident.fname, @resident.lname] - [ '' ]).compact.join(' ')
      end

      def resident_dob
        @resident.dob
      end

      def resident_gender
        @resident.gender
      end

      def resident_doc_full_name
        ([@resident.doc_fname, @resident.doc_lname] - [ '' ]).compact.join(' ')
      end

      def resident_doc_phone1
        @resident.doc_phone1
      end

      def resident_doc_address
        @resident.doc_address
      end

      def resident_doc_city
        @resident.doc_city
      end

      def resident_doc_state
        @resident.doc_state
      end

      def resident_doc_zip
        @resident.doc_zip
      end

      def resident_guard_full_name
        @resident.guard_full_name
      end

      def resident_guard_phone
        @resident.guard_phone
      end

      def resident_desrep_full_name
        @resident.desrep_full_name
      end

      def resident_desrep_phone
        @resident.desrep_phone
      end
    # END -- Resident Helper Methods
    # START -- User Helper Methods
      def user_full_name
        ([@user.fname, @user.lname] - [ '' ]).compact.join(' ')
      end
    # END -- User Helper Methods
    # START -- User Helper Methods
  def user_full_name
    @user_full_name = ([@user.fname, @user.lname] - [ '' ]).compact.join(' ')
    return @user_full_name
  end
# END -- User Helper Methods
# START -- Admin Helper Methods
  def fac_owner_full_name
    @fac_owner_full_name = ([@admin.owner_fname, @admin.owner_lname] - [ '' ]).compact.join(' ')
    return @fac_owner_full_name
  end

  def fac_name
    @fac_name = @admin.fac_name
    return @fac_name
  end

  def fac_lic_num
    @fac_lic_num = @admin.lic_num
    return @fac_lic_num
  end

  def fac_address
    @fac_address = @admin.fac_address
    return @fac_address
  end

  def fac_state
    @fac_state = @admin.fac_state
    return @fac_state
  end

  def fac_zip
    @fac_zip = @admin.fac_zip
    return @fac_zip
  end

  def fac_email
    @fac_email = @admin.fac_email1
    return @fac_email
  end

  def fac_phone
    @fac_phone = @admin.fac_phone1
    return @fac_phone
  end
# END -- Admin Helper Methods

show.html.erb

<tr>
    <td>
        <label for="fac_name">
            <b>Name of Facility/Home</b><br/>
            logger.debug(fac_owner_full_name)

            <%= fac_owner_full_name %>
            <%#= fac_name %>
        </label>
    </td>
    <td>
        <label for="fac_lic_num">
            <b>License Number</b><br />
            <%#= fac_lic_num %>
        </label>
    </td>
    <td>
        <label for="fac_email">
            <b>Email</b><br />
            <%#= fac_email %>
        </label>
    </td>
    <td>
        <label for="fac_phone">
            <b>Phone</b><br />
            <%#= fac_phone %>
        </label>
    </td>
    <td>
        <label for="fac_address">
            <b>Facility Address</b><br />
            <address>
                <%#= fac_address %><br />
                <%#= fac_city " " %><%#= fac_state %><br />
                <%#= fac_zip %>
            </address>
        </label>
    </td>
    <td>
        <label for="fac_owner_full_name">
            <b>Licensee Name</b><br />
            <%#= fac_owner_full_name %>
        </label>
    </td>
</tr>
<tr>
    <td>
        <label for="involved_person_full_name">
            <%= @incident_accident_form.involved_person_full_name %>
        </label>
    </td>

db/schema.rb

create_table "admins", :force => true do |t|
    t.string   "owner_fname"
    t.string   "owner_lname"
    t.string   "lic_num"
    t.text     "fac_address"
    t.string   "fac_city"
    t.string   "fac_state"
    t.string   "fac_zip"
    t.string   "fac_name"
    t.string   "fac_phone1"
    t.string   "fac_phone2"
    t.string   "fac_phone3"
    t.string   "fac_phone4"
    t.string   "fac_phone5"
    t.string   "fac_phone6"
    t.string   "fac_phone7"
    t.string   "fac_phone8"
    t.string   "fac_phone9"
    t.string   "fac_phone10"
    t.string   "fac_phone11"
    t.string   "fac_phone12"
    t.string   "fac_phone13"
    t.string   "fac_phone14"
    t.string   "fac_phone15"
    t.string   "fac_phone16"
    t.string   "fac_phone17"
    t.string   "fac_phone18"
    t.string   "fac_phone19"
    t.string   "fac_phone20"
    t.string   "fac_phone21"
    t.string   "fac_phone22"
    t.string   "fac_phone23"
    t.string   "fac_phone24"
    t.string   "fac_phone25"
    t.string   "fac_phone26"
    t.string   "fac_phone27"
    t.string   "fac_phone28"
    t.string   "fac_phone29"
    t.string   "fac_phone30"
    t.string   "fac_email1"
    t.string   "fac_email2"
    t.string   "fac_email3"
    t.string   "fac_email4"
    t.string   "fac_email5"
    t.string   "fac_email6"
    t.string   "fac_email7"
    t.string   "fac_email8"
    t.string   "fac_email9"
    t.string   "fac_email10"
    t.datetime "created_at",  :null => false
    t.datetime "updated_at",  :null => false
  end

models/admin.rb

class Admin < ActiveRecord::Base
  attr_accessible :fac_address, :fac_city, :fac_name, :fac_state, :fac_zip, :lic_num,
  :owner_fname, :owner_lname, :fac_phone1, :fac_phone2, :fac_phone3, :fac_phone4, :fac_phone5,
  :fac_phone6, :fac_phone7, :fac_phone8, :fac_phone9, :fac_phone10, :fac_phone11, :fac_phone12,
  :fac_phone13, :fac_phone14, :fac_phone15, :fac_phone16, :fac_phone17, :fac_phone18,
  :fac_phone19, :fac_phone20, :fac_phone21, :fac_phone22, :fac_phone23, :fac_phone24,
  :fac_phone25, :fac_phone26, :fac_phone27, :fac_phone28, :fac_phone29, :fac_phone30,
  :fac_email1, :fac_email2, :fac_email3, :fac_email4, :fac_email5, :fac_email6, :fac_email7,
  :fac_email8, :fac_email9, :fac_email10

  # has_many :users
  # validate_on_create :user_count_within_bounds


  private

  def user_count_within_bounds
    return if users.blank?
    errors.add("") if users.length > 10
    errors.add("") if users.length > 25
  end
end

UPDATED 01-17-13 11:30PM EST output from server

Started GET "/residents/1/incident_accident_forms/11" for 127.0.0.1 at 2013-01-17 23:23:57 -0500
Processing by IncidentAccidentFormsController#show as HTML
  Parameters: {"resident_id"=>"1", "id"=>"11"}
  Resident Load (1.0ms)  SELECT "residents".* FROM "residents" WHERE "residents"."id" = $1 LIMIT 1  [["id", "1"]]
  IncidentAccidentForm Load (1.3ms)  SELECT "incident_accident_forms".* FROM "incident_accident_forms" WHERE "incident_accident_forms"."resident_id" = 1 AND "incident_accident_forms"."id" = $1 LIMIT 1  [["id", "11"]]
  Rendered incident_accident_forms/show.html.erb within layouts/application (6.3ms)
Completed 500 Internal Server Error in 14ms

ActionView::Template::Error (undefined method `owner_fname' for nil:NilClass):
    12:                             <b>Name of Facility/Home</b><br/>
    13:                             logger.debug(fac_owner_full_name)
    14: 
    15:                             <%= fac_owner_full_name %>
    16:                             <%= fac_name %>
    17:                         </label>
    18:                     </td>
  app/helpers/application_helper.rb:74:in `fac_owner_full_name'
  app/views/incident_accident_forms/show.html.erb:15:in `_app_views_incident_accident_forms_show_html_erb__403107391156795487_2183850600'


  Rendered /Users/beracus/.rvm/gems/ruby-1.9.3-p362/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.5ms)
  Rendered /Users/beracus/.rvm/gems/ruby-1.9.3-p362/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
  Rendered /Users/beracus/.rvm/gems/ruby-1.9.3-p362/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (9.4ms)

UPDATED incident_accident_forms_controller.rb

class IncidentAccidentFormsController < ApplicationController
  before_filter :get_resident
  before_filter :get_admin

  def index
    @incident_accident_forms = @resident.incident_accident_forms
  end

  def new
    @incident_accident_form = @resident.incident_accident_forms.build
  end

  def create
    @incident_accident_form = @resident.incident_accident_forms.build(params[:incident_accident_form])
    if @incident_accident_form.save
      redirect_to [@admin, @resident, @incident_accident_form] #, flash[:success] = "Form was created!"
    else
      render 'new', flash[:error] = "There was a problem with the form"
    end
  end

  def show
    @incident_accident_form = @resident.incident_accident_forms.find(params[:id])
  end

  def update
    @incident_accident_form = @resident.incident_accident_forms.find(params[:id])
    if @incident_accident_form.update_attributes(params[:incident_accident_form])
      flash[:success] = "Form updated!"
      redirect_to controller: 'residents', action: 'show', id: params[:id]
    else
      render 'edit', flash[:error] = "Unable to update form"
    end
  end

  def edit
    @incident_accident_form = @resident.incident_accident_forms.find(params[:id])
  end

  def destroy
    @incident_accident_form = @resident.incident_accident_forms.find(params[:id])
    @incident_accident_form.destroy
    flash[:notice] = "You sure?"
    redirect_to resident_incident_accident_forms_path
  end

  private
  # get_resident converts the resident_id given by the routing
  # into an @resident object, for use in this controller & coresponding views
  def get_resident
    @resident = Resident.find(params[:resident_id])
  end

  def get_admin
    @admin = Admin.find(params[:admin_id])
  end
end

2 Answers 2

1

This error is essentially saying @admin is nil since you are trying to call the owner_fname method on @admin in fac_owner_full_name. I'd double check you are actually setting that variable in your controller.

This seems to be confirmed, as your IncidentAccidentFormsController#show action isn't making any SQL calls to your admins table.

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

8 Comments

I have made an update of my server output. You were spot on with your assessment, I had a few typos/errors in my admin_controller file. I have corrected (or think I have corrected) the errors, and am still receiving the same error. The admin_controller.rb file has been added at the end for viewing.
@SirRamonGabriel I think you want to post the IncidentAccidentFormsController since it's that show action in that controller that is failing. You want that show method to be specifying an @admin variable.
I have removed admin_controller and replaced it with incident_accident_forms_controller.
As suspected, there's no @admin variable. Is an Admin model available from either your @incident_accident_form or @resident through one of their associations?
I haven't associated Admin with either. Ok, so it sounds like you're saying to me that I need an association between the models behind each view that I need to share helper methods with, is that correct? If that is the case, is it ok to nest my routes more than one level deep in that scenario? PS. Routes.rb was added to demonstrate no model associations with Admin. Thanks for your time by the way.
|
0

Change your methods to this format:

def resident_full_name
  @resident_full_name = ([@resident.fname, @resident.lname] - [ '' ]).compact.join(' ')
  return @resident_full_name
end

1 Comment

I have made updates to my question to reflect your suggestion. Is it idiomatic to be explicit in Rails? If so (or even if not) thank you for the suggestion, it looks cleaner.

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.