Live2Learn Live2Learn - 2 months ago 12
Ruby Question

Ruby on Rails - Undefined Method - Rails 3.2.13

Unfortunately, our Ruby Developer is out of town, and we have an issue that needs a workaround fix until he can return. I have been tasked with finding it.

I am attempting to access one of our database views, hr_accounts_view, that contains the necessary data we need to incorporate into our existing page, which relies on an entirely different table. All that we are attempting to pull in, is the Status column from our view, so I have done my very best to keep the model/controller/view as simple as possible.

My hr_account_view model:

class HrAccountView < ActiveRecord::Base

self.table_name = 'hr_accounts_view'

include Savable

# belongs to :hr_account
belongs to :employee, foreign_key: :id


validates :Status, presence: true



end


Hr_Account_Views Controller:

class HrAccountViewsController < ApplicationController

load_and_authorize_resource

def show
@HrAccountView.Status = HrAccountView.Status
end

private

end


I've also separated the view out into it's own folder within the views, in an attempt to make it correlate with the new controller. We are rendering this page within our general show page.

Page being called from our show, hraccountsview/_apptment.html.erb:

<table>
<tr>
<td>
Status<br />
<%= display_text_field @HrAccountView.Status %>
</td>
</tr>
</table>


I've gone through and read a ton of posts referencing this issue, and it looks like the issue would be a declaration in the controller missing, but I've tried all sorts of combinations. Unfortunately, the most I've really done in ruby and rails is minor page changes, and some search functions, but never anything having to add an additional table/view.

According to the error message, I have an undefined method 'Status' for nill:NillClass, and it is occurring on line 5 of my page that's being rendered. If I throw in a rescue nil, the page will at least display the header, but no data. The record being previewed has a populated record for the Status column presently. This is leading me to believe there's an issue with the model instead of the controller, but at this point, I've been spinning my wheels trying to figure out where the missing connection is at.

Does anyone have an idea of where I'm missing the connection?

You're help is greatly appreciated!

Rails Version 3.2.13
Database Version : SQL 2012

Answer

There are a few conventions that may help:

Attributes (the model version of a column) are specified in lowercase. So:

validates :status, presence: true

Same for instance variables. E.g.:

def show
  @status = HrAccountView.Status
end

I suspect that HrAccountView.Status may also be wrong, though possibly not. We would need to see what else is going on in the controller. Typically there is some operation doing a find on the model using a parameter value from the request.

The variable name in the controller and the view must match:

<%= display_text_field @status %>

In Ruby a capitalized string (e.g. Status) is a constant, a lowercase string is a reference to a variable or a method. If prefixed with '@' it is a reference to an instance variable.