Ruby on rails 未定义的方法`在创建之前';
我有一个用户模型和一个公司模型链接如下:Ruby on rails 未定义的方法`在创建之前';,ruby-on-rails,ruby-on-rails-3.2,Ruby On Rails,Ruby On Rails 3.2,我有一个用户模型和一个公司模型链接如下: class User < ActiveRecord::Base belongs_to :company accepts_nested_attributes_for :company end class Company < ActiveRecord::Base has_many :users end <%= simple_form_for @user, :html => { :class => 'for
class User < ActiveRecord::Base
belongs_to :company
accepts_nested_attributes_for :company
end
class Company < ActiveRecord::Base
has_many :users
end
<%= simple_form_for @user, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :email, :required => true, :placeholder => "user@domain.com" %>
<%= f.input :password, :required => true %>
<%= f.input :password_confirmation, :required => true %>
<h2>Company info</h2>
<%= simple_fields_for :company, :html => { :class => 'form-horizontal' } do |fa| %>
<%= fa.input :name %>
<%= fa.input :url %>
<%= fa.input :description, :as => :text, :input_html => { :cols => 60, :rows => 3 } %>
<%= fa.input :logo %>
<%= fa.input :industry %>
<%= fa.input :headquarters %>
<% end %>
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
root_url, :class => 'btn' %>
</div>
<% end %>
问题是:当访问/users/new时,我遇到以下错误:
undefined method `before_create' for UsersController:Class
怎么了?我检查了一下,在没有人反对创建之前,我使用的是Rails 3.2.8。这可能是我的create_company
方法的愚蠢之处,但我不明白为什么
非常感谢你的帮助 before\u create是一个属于ActiveRecord的钩子方法 before_filter是属于控制器的钩子方法
因此,我建议您在弄清楚哪个是哪个之后重新构建代码。^ 谢谢!我以前以为她是个选择者。例如,在索引之前,在显示之前,这两种方法都有效。所以我用这个修改了它,现在我没有这个错误消息。在过滤器:创建公司之前,:only=>:create
undefined method `before_create' for UsersController:Class