1

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.

Community
  • 1
  • 1
Mike T
  • 4,747
  • 4
  • 32
  • 52
  • What file is the error occurring in? – Jake Smith Apr 04 '13 at 19:05
  • developers/_fields.html.erb I'll edit the question too. Thanks for pointing that out. – Mike T Apr 04 '13 at 19:06
  • This might not be it, but it looks like the form is assuming Developer has a name, but User is the one with a name. Is there a way to put `<%= f.label [user in developer object] :name %>` ? Or maybe the form should be for @user since that seems to be the more broad object? I'm just now getting the hang of these ruby on rails forms, so I'm hesitant with my suggestions here. – Jake Smith Apr 04 '13 at 19:14

1 Answers1

2

You are trying to access the user attributes in the scope of the developer parent. Use fields_for to modify the scope:

<%= render 'shared/error_messages' %>
<%= f.fields_for :user do |u| %>
  <%= u.label :name %>
  <%= u.text_field :name %>
<% end %>

You'll also probably want to make the Developer model accept nested attributes for the User. Have a look at some examples in the documentation.

PinnyM
  • 35,165
  • 3
  • 73
  • 81
  • 1
    dang, should have had the confidence to post an answer! Good to know I was on the right track though! I'm saving this SO question for future reference. – Jake Smith Apr 04 '13 at 19:23
  • Thanks! It's still broken, but because of auth checks now. The issue I had is resolved. – Mike T Apr 04 '13 at 19:47