我是编码的新手,所以这可能是一个简单的问题。
一个月前我开始使用RoR。不幸的是,我碰到了碰撞,似乎无法克服它。我已经尝试过寻求其他SO问题寻求帮助,但我仍然是新手,所以编码建议对我来说仍然有些陌生。我希望有人可以把事情变得更加新手友好。
我想要做的是让我的网站为每个注册用户设置一个配置文件。这将是一个私人配置文件,只有用户和管理员才能访问。在用户注册/登录后,我希望将他们重定向到他们的个人资料,在那里他们可以编辑年龄和体重等信息。
我花了最近3天试图找出如何为每个新用户创建个人资料页面。我查看了Devise github自述文件,但我还是难过。
我已经生成了用户控制器和用户视图,但我甚至不知道自从我设计以来是否需要执行这些步骤。你们可以给我的任何帮助都将不胜感激。
这是指向我的github页面的链接 - https://github.com/Thefoodie/PupPics
谢谢
答案 0 :(得分:10)
根据Kirti的回答,您需要实际拥有profile
重定向到:
<强>模型强>
#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
<强>模式强>
profiles
id | user_id | name | birthday | other | info | created_at | updated_at
<强>路线强>
#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
查看强>
#app/views/profiles/edit.html.erb
<%= form_for @profile do |f| %>
<% @attributes.each do |attr| %>
<%= f.text_field attr.to_sym %>
<% end %>
<% end %>
然后你需要使用Kirti发布的after_sign_in_path
内容
<强>更新强>
以下是您要使用的迁移:
# 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
答案 1 :(得分:8)
首先,您需要为after_sign_in_path_for
中的资源设置after_sign_up_path_for
和ApplicationController
,这将指向profile
页面。
然后,您需要创建一个controller
来呈现profile
页面。
例如:(根据您的要求更改)
在ApplicationController
中定义路径
def after_sign_in_path_for(resource)
profile_path(resource)
end
def after_sign_up_path_for(resource)
profile_path(resource)
end
在ProfilesController
## You can skip this action if you are not performing any tasks but,
## It's always good to include an action associated with a view.
def profile
end
另外,请确保为用户个人资料创建view
。