Note that there are some explanatory texts on larger screens.

plurals
  1. POundefined method `before_create'
    primarykey
    data
    text
    <p>I have a User model and a Company model linked like this:</p> <pre><code>class User &lt; ActiveRecord::Base belongs_to :company accepts_nested_attributes_for :company end class Company &lt; ActiveRecord::Base has_many :users end </code></pre> <p>On the sign in page, I want the user to set up both his info (mail, password) and his company info (several fields). So my form looks like this:</p> <pre><code>&lt;%= simple_form_for @user, :html =&gt; { :class =&gt; 'form-horizontal' } do |f| %&gt; &lt;%= f.input :email, :required =&gt; true, :placeholder =&gt; "user@domain.com" %&gt; &lt;%= f.input :password, :required =&gt; true %&gt; &lt;%= f.input :password_confirmation, :required =&gt; true %&gt; &lt;h2&gt;Company info&lt;/h2&gt; &lt;%= simple_fields_for :company, :html =&gt; { :class =&gt; 'form-horizontal' } do |fa| %&gt; &lt;%= fa.input :name %&gt; &lt;%= fa.input :url %&gt; &lt;%= fa.input :description, :as =&gt; :text, :input_html =&gt; { :cols =&gt; 60, :rows =&gt; 3 } %&gt; &lt;%= fa.input :logo %&gt; &lt;%= fa.input :industry %&gt; &lt;%= fa.input :headquarters %&gt; &lt;% end %&gt; &lt;div class="form-actions"&gt; &lt;%= f.submit nil, :class =&gt; 'btn btn-primary' %&gt; &lt;%= link_to t('.cancel', :default =&gt; t("helpers.links.cancel")), root_url, :class =&gt; 'btn' %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>My user model has a <code>company_id:integer</code> field. So logically, when I sign in the user, the first thing to do is to create the Company before the User and then give to the user creation model the appropriate <code>company_id</code>. So I wrote this:</p> <pre><code>class UsersController &lt; ApplicationController before_create :create_company def new @user = User.new end def create @user = User.new(params[:user]) if @user.save redirect_to root_url, :notice =&gt; "Registration successful." else render :action =&gt; 'new' end end private def create_company @company = Company.new(params[:company]) if @company.save self.company_id = @company.id else render :action =&gt; 'new' end end end </code></pre> <p>Problem is: when accessing /users/new I get this error:</p> <pre><code>undefined method `before_create' for UsersController:Class </code></pre> <p>What's going wrong? I checked, before_create has not been deprecated, I'm in Rails 3.2.8. This is probably something stupid with my <code>create_company</code> method but I can't figure why...</p> <p>Thanks a lot for helping!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload