尝试使用 application_helper 文件中的方法时收到 noMethodError。
看起来辅助方法本身很好,但是辅助方法中的代码是触发错误的原因。我已经仔细检查了我的模型和模式文件,以确保我的属性列出正确。对此有一双新的眼睛会很受欢迎。
这是我的代码...
application_helper.rb
PS。我在这里展示了两组辅助方法,因为这第一组(驻留辅助方法)在其预期的视图中工作正常。
# 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
显示.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>
数据库/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
模型/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
更新于 13 年 1 月 17 日晚上 11:30 东部标准时间从服务器输出
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)
更新的 event_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