我对编码很陌生,所以这可能是一个简单的问题。
大约一个月前,我开始使用RoR。不幸的是,我撞到了一个颠簸,似乎无法克服它。我试着寻找其他的问题来寻求帮助,但我仍然是新手,所以编码建议对我来说还是有点陌生。我希望有人能把事情说得更友好些。
我想做的是让我的网站为每一个注册的用户建立个人资料。这将是一个只有用户和管理员才能访问的私有配置文件。在用户注册/登录后,我希望他们被重定向到他们的个人资料,在那里他们可以编辑像年龄和体重这样的信息。
在过去的3天里,我一直在努力弄清楚如何为每个新用户创建一个配置文件页面。我看了github自述文件,但我还是很困惑。
我已经生成了一个用户控制器和用户视图,但我甚至不知道是否需要执行这些步骤,因为我已经设计了这些步骤。如果你们能给我什么帮助,我会很感激的。
这里有一个指向我的github页面的链接- https://github.com/Thefoodie/PupPics
谢谢
发布于 2014-02-24 10:06:01
至于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发布于 2014-02-23 23:26:43
首先,您需要在after_sign_in_path_for中为您的资源设置ApplicationController和after_sign_up_path_for,这将指向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。
https://stackoverflow.com/questions/21976002
复制相似问题