1
votes

Here is my models:

class User <  ActiveRecord::Base
  has_one :worker, :class_name => 'Worker', :foreign_key => :worker_id
  devise :database_authenticatable
  accepts_nested_attributes_for :worker
  attr_accessible  :worker_id, :email, :password, :password_confirmation, :remember_me, :workers_attributes, :worker_attributes, :name, :worker
end
class Worker < User
devise :database_authenticatable, :registerable
belongs_to :user
attr_accessible :name, :worker, :workers
end

I am trying to add the field name to the registretion form at http://localhost:3000/workers/sign_up

The sign up form

<h2>Create Worker</h2>
<%= form_for resource, :as => resource_name, :url => registration_path(resource_name) do |f| %>

  <%= devise_error_messages! %>
 <table summary="Subject form fields">
      <tr>
        <th>Name:</th>
        <td><%= f.text_field :name %></td>
      </tr>
         <tr>
  <th><%= f.label :email %></th>
   <td><%= f.text_field :email %></td>
   </tr>
 <tr>
  <th><%= f.label :kodeord %></th>
  <td><%= f.password_field :password %></td>
  </tr>
  <tr>
  <th><%= f.label :bekraeft_kodeord %></th>
  <td><%= f.password_field :password_confirmation %></td>
  </tr>
   </table>
  <p><%= f.submit "Create Worker" %></p>
<% end %>
<%= render :partial => "devise/shared/links" %>

But i get a template error: Model Worker does not respond to name And how do i create the association between User and Worker?

Best regards, Rails beginner

1
Are there different kinds of user? Do you need Single table inheritance? Do you need to access a worker through: user.worker? Just for me to understand your question better. - apneadiving
I need a STI to be able to have one single login form that Workers and Companies can both use. I relly want to be able to display diffrent views for Workers and Companies. Then i properly need to access a worker through user.worker. In my routes.rb i have devise_for :users, :companies, :workers. - Rails beginner

1 Answers

8
votes

I hope I understand well: I think you haven't understand the STI concept yet.

Let's try to make it clearer.

The classes you derive from an orinal model inherits everything from it. Your original model should look like this:

class User <  ActiveRecord::Base
  devise :database_authenticatable
  attr_accessible :email, :password, :password_confirmation, :remember_me    
end

To really be a STI, you've to generate a migration to include "type" to your model. Simply type:

rails g migration add_type_to_users type:string
rake db:migrate

Then set up you worker model which is really simple:

class Worker < User
end

As you made, include in your routes.rb file:

devise_for :users, :companies, :workers

Now you're done!

Go to workers/sign_up, create an account and the go back to your terminal.

From here, type rails c to start the console.

Now try: User.all.last, you should see the account you just created with a 'worker' type

And try: Worker.last, here again, you find the latest account created.

Please remember: Rails is as great as simple :)