【发布时间】:2013-04-04 18:55:42
【问题描述】:
让我解释一下我要做什么,然后问我的问题。我完全是 Ruby/Rails 的新手,但我是一位经验丰富的开发人员。
我想要什么:我想要开发人员、客户和管理员。我想要一个通用的身份验证机制。
似乎最好的方法:我认为最好的建模方式是让用户处理身份验证,然后进行某种继承或扩展每个人的功能用户类型。
我正在尝试什么:我正在尝试一种称为多态关联 (http://guides.rubyonrails.org/association_basics.html#polymorphic-associations) 的东西,但我错过了一些东西。目前如果我去/developers/new,我会得到以下内容
in developers/_fields.html.erb where line #4 raised:
undefined method `name' for #<Developer:0x00000002bafd18>
user.rb sn-p
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 sn-p
class Developer < ActiveRecord::Base
attr_accessible :skype_name
has_one :user, :as => :profile, :dependent => :destroy
accepts_nested_attributes_for :user
end
routes.rb sn-p
resources :developers
Developer#new 控制器 sn-p
def new
@developer = Developer.new
@developer.user = User.new
end
new.html.erb 面向开发人员的 sn-p
<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>
终于 _fields.html.erb sn-p
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
...
我找到的最接近的问题是this,但我从他们的回答中尝试了一些方法,但没有帮助。
我可以创建一个新的用户和开发人员,通过 rails 控制台相互链接,但我不知道我在网络上做错了什么。
任何帮助将不胜感激。
【问题讨论】:
-
错误发生在哪个文件?
-
developers/_fields.html.erb 我也会编辑问题。感谢您指出这一点。
-
这可能不是它,但看起来表单假设 Developer 有一个名称,但 User 是一个有名称的人。有没有办法输入
<%= f.label [user in developer object] :name %>?或者也许该表单应该用于@user,因为这似乎是更广泛的对象?我刚刚掌握了这些 ruby on rails 表单的窍门,所以我在这里对我的建议犹豫不决。
标签: ruby-on-rails ruby-on-rails-3 polymorphic-associations