Ruby on Rails 4、设计和个人资料页面 [英] Ruby on Rails 4, Devise, and profile pages

查看:22
本文介绍了Ruby on Rails 4、设计和个人资料页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编码新手,所以这可能是一个简单的问题.

I am new to coding, so this is probably a simple question.

我大约一个月前开始使用 RoR.不幸的是,我遇到了一个颠簸,似乎无法克服它.我已经尝试查看其他 SO 问题寻求帮助,但我仍然是新手,所以编码建议对我来说仍然有点陌生.我希望有人能用更适合新手的术语来表达.

I started using RoR about a month ago. Unfortunately, I've hit a bump and can't seem to get over it. I've tried looking at other SO questions for help, but I'm still a novice, so the coding suggestions still look a little foreign to me. I was hoping someone could put things into more novice-friendly terms.

我想要做的是让我的网站为每个注册的用户设置一个配置文件.这将是一个只有用户和管理员才能访问的私人配置文件.用户注册/登录后,我希望他们被重定向到他们的个人资料,在那里他们可以编辑年龄和体重等信息.

What I want to do is have my website set up a profile for each user that signs up. This would be a private profile that only the user and admins would have access to. After the user signs up/logs in, I'd like for them to be redirected to their profile, where they can edit info like age and weight.

在过去的 3 天里,我一直在试图弄清楚如何为每个新用户创建个人资料页面.我查看了 Devise github 自述文件,但我仍然很难过.

I've spent the last 3 days trying to figure out how to get a profile page created for each new user. I looked at the Devise github readme file, but I'm still stumped.

我已经生成了用户控制器和用户视图,但我什至不知道是否需要执行这些步骤,因为我已经设计好了.你们能给我的任何帮助将不胜感激.

I've generated a users controller and users view, but I don't even know if I needed to do those steps since I have devise. Any help you guys could give me would be appreciated.

这是我的 github 页面的链接 - https://github.com/Thefoodie/PupPics

Here's a link to my github page - https://github.com/Thefoodie/PupPics

谢谢

推荐答案

根据 Kirti 的回答,您实际上需要一个 profile 重定向到:

Further to Kirti's answer, you'll need to actually have a profile to redirect to:

模型

#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 %>

然后您需要使用 after_sign_in_path Kirti 发布的东西

You'll then need to employ the after_sign_in_path stuff Kirti posted

更新

这是您将使用的迁移:

# 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

这篇关于Ruby on Rails 4、设计和个人资料页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆