ActiveRecord的::关系:: ActiveRecord_Relation_Requisition:类?

时间:2014-03-21 08:14:26

标签: ruby-on-rails activerecord ruby-on-rails-4

Questions:

  • 这意味着什么?
  • 为什么我无法打开我的表单,即使它有resources requisition

我的日志:

ActionView::Template::Error (undefined method `errors' for #<ActiveRecord::Relation []>):
2: <div class = "well">
3:  <h2>Requisition slip</h2>
4:          <d class ="red">
5:                  <% if @requisition.errors.any? %>
6:                  <div id="error_explanation">
7:                          <h2><%=pluralize(@requisition.errors.count, "error") %>
8:prohibited this<%=@requisition.class.to_s.underscore.humanize.downcase %>

app/views/employee/_form.html.erb:5:in `_app_views_employee__form_html_erb___3453800_32433336'
app/views/employee/_content.html.erb:88:in `_app_views_employee__content_html_
erb___107804244_32353044'
app/views/employee/emain.html.erb:11:in `_app_views_employee_emain_html_erb___150956366_31927824'


Rendered  

F:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered  
F:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
Rendered     
F:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack4.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (17.0ms)

控制器:

class RequisitionsController < ApplicationController
def new
@requisition = Requisition.new
@realasset = Realasset.new
@location = Location.new
end

def create
@requisition = Requisition.new(params[:requisition])
if @requisition.save
 Requisitionmailer.requisition_confirmation(@requisition).deliver
  if emp_signed_in?

  flash[:success] = " #{current_emp.username.capitalize} Your Requisition Submitted successfull"
  redirect_to emain_path
end
else
  if emp_signed_in?
  flash[:warning] = "Unable to send requisition try again"
  render "new"
elsif authorize_signed_in?
  flash[:warning] = "Unable to send requisition try again"
  redirect_to emain_path
  end

end
end

def show
@requisition = Requisition.find(params[:id])

end

def index
@requisition = Requisition.all
@requisition = Requisition.order("name").page(params[:page]).per(5)
 end

def destroy
@requisition = Requisition.find(params[:id])
@requisition.destroy
redirect_to requisitions_path

end
end

2 个答案:

答案 0 :(得分:0)

这可能是因为@requisition是零。您可以使用@requisition.present?之类的支票跳过@requisition.errors,以防它为零。

答案 1 :(得分:0)

errors方法可用于Model对象(继承自ActiveRecord :: Base)。 您正尝试访问errors对象上的ActiveRecord:Relation方法,该对象将是Model对象的集合。

所以,是的,你肯定会得到这个错误。

我不确定您收到错误的操作:

如果您在相关操作中将@requisition定义为:

@requistion = Requisition.find(params[:id])

@requisition = Requisition.new

然后当前的代码将起作用。

但是如果你已经定义了

@requisition = Requisition.all ## or some other method which returns a collection ActiveRecord::Relation object

然后你会得到错误。