我是编码新手,所以这可能是一个简单的问题。
我大约一个月前开始使用 RoR。不幸的是,我遇到了坎坷,似乎无法克服。我尝试过查看其他 SO 问题来寻求帮助,但我仍然是新手,所以编码建议对我来说仍然有点陌生。我希望有人能用更适合新手的术语来表述。
我想做的是让我的网站为每个注册的用户建立一个个人资料。这将是只有用户和管理员才能访问的私人配置文件。用户注册/登录后,我希望他们被重定向到他们的个人资料,他们可以在其中编辑年龄和体重等信息。
在过去的三天里,我一直在尝试弄清楚如何为每个新用户创建个人资料页面。我查看了 Devise github 自述文件,但我仍然感到困惑。
我已经生成了一个用户控制器和用户视图,但我什至不知道是否需要执行这些步骤,因为我已经设计了。你们能给我的任何帮助将不胜感激。
这是我的 github 页面的链接 -https://github.com/Thefoodie/PupPics
谢谢
除了 Kirti 的回答之外,你实际上需要有一个profile
重定向到:
Models
#app/models/profile.rb
Class Profile < ActiveRecord::Base
belongs_to :user
end
#app/models/user.rb
Class User < ActiveRecord::Base
has_one :profile
before_create :build_profile #creates profile at user registration
end
Schema
profiles
id | user_id | name | birthday | other | info | created_at | updated_at
Routes
#config/routes.rb
resources :profiles, only: [:edit]
控制器
#app/controllers/profiles_controller.rb
def edit
@profile = Profile.find_by user_id: current_user.id
@attributes = Profile.attribute_names - %w(id user_id created_at updated_at)
end
View
#app/views/profiles/edit.html.erb
<%= form_for @profile do |f| %>
<% @attributes.each do |attr| %>
<%= f.text_field attr.to_sym %>
<% end %>
<% end %>
然后你需要使用after_sign_in_pathKirti 发布的内容
Updated
这是您要使用的迁移:
# db/migrate/[[timestamp]]_create_profiles.rb
class CreateProfiles < ActiveRecord::Migration[5.0]
def change
create_table :profiles do |t|
t.references :user
# columns here
t.timestamps
end
end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)