我无法理解,但我的新观点开始崩溃。
我有模型票据
class Ticket < ActiveRecord::Base
attr_accessible :body, :department, :email, :name, :reference, :subject, :status
has_many :replies, :dependent => :destroy
和简单的新动作
def new
logger.debug "#{@ticket}"
@ticket = Ticket.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @ticket }
end
end
和new.haml.html(抱歉错误的haml格式):
= form_for @ticket do |f|
- if @ticket.errors.any?
#error_explanation
%h2= "#{pluralize(@ticket.errors.count, "error")} prohibited this ticket from being saved:"
%ul
- @ticket.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :name
= f.text_field :name
.field
- if current_admin
= f.label :email
= f.text_field :email, :value => "#{current_admin.email}", :disabled => :true
- else
= f.label :email
= f.text_field :email
.field
= f.label :department
= f.select :department, options_for_select(["First department","Second department","Third department"])
.field
= f.label :subject
= f.text_field :subject
.field
= f.label :body
= f.text_area :body
- if current_admin
.field
= f.label :status
= f.select :status, options_for_select(["Waiting for Staff Response","Waiting for Customer","On Hold", "Cancelled", "Completed"])
.actions
= f.submit 'Save'
错误:
ActionView::Template::Error (undefined method `model_name' for NilClass:Class):
1: = form_for @ticket do |f|
2: - if @ticket.errors.any?
3: #error_explanation
4: %h2= "#{pluralize(@ticket.errors.count, "error")} prohibited this ticket from being saved:"
app/views/tickets/_form.html.haml:1:in
`_app_views_tickets__form_html_haml__659380982_96991070'
Rendered /home/denmed/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.8/lib
/action_dispatch/middleware/templates/rescues/_trace.erb (5.9ms)
Rendered /home/denmed/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.8/lib
/action_dispatch/middleware/templates/rescues/_request_and_response.erb (4.1ms)
Rendered /home/denmed/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.8/lib
/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (42.2ms)
和routes.rb
resources :tickets do
resources :comments
end
我可以从控制台创建Ticket,那么问题出在哪里?有人可以建议错误在哪里吗?
答案 0 :(得分:0)
问题在于@ticket
以某种方式得到“诽谤”。那是因为当你看到form_for
def form_for(record, options = {}, &proc)
# ....
object = record.is_a?(Array) ? record.last : record
object_name = options[:as] || ActiveModel::Naming.param_key(object)
# ....
end
在ActiveModel::Naming.param_key(object)
被调用的地方,我们最终采用以下方法
def model_name_from_record_or_class(record_or_class)
(record_or_class.is_a?(Class) ? record_or_class : record_or_class.class).model_name
end
在那里你可以看到你是否将nil传递给这个方法它会抛出你发布的错误 - 所以检查@ticket获取nil
的地点和原因
为此,您可能需要关注this guide,它解释了如何调试rails应用程序。
你可能也想换掉那些线。
def new
logger.debug "#{@ticket}"
@ticket = Ticket.new
end
因为它现在的方式你不会得到任何有用的输出。