Let me explain what I'm trying to do, then ask my question. I'm a total newbie with Ruby/Rails, but an experienced developer.
What I want: I want to have developers, customers and admins. I want a common authentication mechanism.
What seems to be best: The way I think is best to model this is by having a User that deals with the authentication and then some kind of inheritance or something that extends the functionality for each of the user types.
What I'm trying: I'm trying a thing called polymorphic association (http://guides.rubyonrails.org/association_basics.html#polymorphic-associations) but I've missed something. At the moment if I go to /developers/new
, I get the following
in developers/_fields.html.erb where line #4 raised:
undefined method `name' for #<Developer:0x00000002bafd18>
user.rb snippet
class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation, :profile_id, :profile_type
has_secure_password
belongs_to :profile, :polymorphic => true
...
end
developer.rb snippet
class Developer < ActiveRecord::Base
attr_accessible :skype_name
has_one :user, :as => :profile, :dependent => :destroy
accepts_nested_attributes_for :user
end
routes.rb snippet
resources :developers
Developer#new controller snippet
def new
@developer = Developer.new
@developer.user = User.new
end
new.html.erb snippet for Developers
<div class="row">
<div class="span6 offset3">
<%= form_for (@developer) do |f| %>
<%= render 'fields', f: f %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
finally _fields.html.erb snippet
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
...
The closest question I've found was this but I tried a few things from their answer but it didn't help.
I can create a new User and Developer that link to each other via the rails console but I can't figure out what I'm doing wrong for the web.
Any help would be much appreciated.