Ruby on Rail:向 Devise 用户添加多态地址

发布于 2024-12-09 11:52:53 字数 1547 浏览 0 评论 0原文

ROR新手在这里。 :-)

我正在使用 Devise 进行身份验证,并将 first_name 和 last_name 添加到 devise 创建的用户模型中。我还创建了地址模型:

create_table :addresses do |t| t.字符串:第1行 t.字符串:第2行 t.string :城市 t.string :状态 t.integer :addressable_id t.string :addressable_type

  t.timestamps
end

add_index :addresses, [:addressable_type, :addressable_id], :unique => true 

在views/shared/_address.html.erb 处结束


部分






user.rb

has_one :地址, :as => :addressable

# 包括默认设备模块。其他可用的有: # :token_authenticatable、:encryptable、:confirmable、:lockable、:timeoutable 和 :omniauthable 设计:database_authenticatable,:可注册, :recoverable, :rememberable, :trackable, :validatable

# 为模型设置可访问(或受保护)的属性 attr_accessible :电子邮件、:密码、:密码确认、:记住我、:名字、:姓氏 attr_accessible :address_attributes

接受_nested_attributes_for :address, :update_only => true

在views/devise/registrations/new.html.erb 和edit.html.erb 添加了以下几行


<% f.fields_for :address do |address|%>; <%= 渲染:部分 => '共享/地址', :locals => {:f =>地址}%>

<%结束%>

<%结束%>


<% f.fields_for :address |address|%> <%= 渲染:部分 => '共享/地址', :locals => {:f =>地址}%>

<%结束%>

<%结束%>


现在我想让 Devise 在新用户注册时创建一个地址。用户可以在登录后填写地址。显然缺少某些内容或者我做错了。地址字段在注册或编辑时均不显示。

请分享你的智慧。提前致谢。

ROR newbie here. :-)

I am using Devise for authentication and have added first_name and last_name to the User model created by devise. I have also created Address model:

create_table :addresses do |t|
t.string :line1
t.string :line2
t.string :city
t.string :state
t.integer :addressable_id
t.string :addressable_type

  t.timestamps
end

add_index :addresses, [:addressable_type, :addressable_id], :unique => true 

end


a partial at views/shared/_address.html.erb


user.rb

has_one :address, :as => :addressable

# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :firstname, :lastname
attr_accessible :address_attributes

accepts_nested_attributes_for :address, :update_only => true

added the following lines at views/devise/registrations/new.html.erb and edit.html.erb


<% f.fields_for :address do |address|%>
<%= render :partial => 'shared/address', :locals => {:f => address}%>

<% end %>

<% end %>


<% f.fields_for :address do |address|%>
<%= render :partial => 'shared/address', :locals => {:f => address}%>

<% end %>

<% end %>


Now I want to have Devise create an Address when a new user sign up. And the user could fill in the address after sign in. Apparently something is missing or I am doing it wrong. The address fields are not showing at sign_up nor edit.

Please share your wisdom. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瑶 2024-12-16 11:52:53

fields_for 的一般行为是关联对象不会显示在表单中,除非它已经存在。例如,如果我们在 Devise 上下文之外说话,那么为了让地址显示在新用户表单上,您将得到类似的内容:

# users_controller.rb
def new
   @user = User.new
   @user.address = Address.new
end

然后地址将显示在表单上。

鉴于这是 Devise 管理的表单,并且您无法直接访问必要的控制器,我猜最好的办法是使用 ActiveRecord 回调来实例化地址(如果该地址尚不存在)。例如类似的东西

# user.rb
after_initialize :create_address

def create_address
    self.address = Address.new if self.address.nil?
end

The general behavior of fields_for is that the associated object will not show in the form unless it already exists. E.g. if we were speaking outside the context of Devise, then to get the address to show up on the new user form you would have something like:

# users_controller.rb
def new
   @user = User.new
   @user.address = Address.new
end

and then the address would display on the form.

Given that this is a Devise-managed form and you do not have direct access to the necessary controller, I'm guessing the best thing to do is to use an ActiveRecord callback to instantiate an address if it doesn't exist already. E.g. something like

# user.rb
after_initialize :create_address

def create_address
    self.address = Address.new if self.address.nil?
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文