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

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

问题描述

我很熟悉编码,所以这可能是一个简单的问题。



我一个月前开始使用RoR。不幸的是,我遇到了一个颠簸,似乎无法克服。我已经尝试了其他SO问题的帮助,但我仍然是一个新手,所以编码建议对我来说看起来有点陌生。我希望有人可以把事情变成更多的新手友好的条款。



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



花了最后3天试图找出如何获取为每个新用户创建的个人资料页面。我看着Devise github自述文件,但是我仍然很困惑。



我已经生成了一个用户控制器和用户视图,但是我甚至不知道我是否需要做这些步骤,因为我有设计。任何帮助,你们可以给我,将不胜感激。



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



谢谢

解决方案

除了Kirti的答案,您需要实际拥有个人资料才能重定向到:



模型

 #app / models / profile.rb 
类简档< ActiveRecord :: Base
belongs_to:user
end

#app / models / user.rb
Class User< ActiveRecord :: Base
has_one:profile
before_create:build_profile#在用户注册时创建配置文件
end






架构

 个人资料
id | user_id |名称|生日|其他|信息| created_at | updated_at






路线 / p>

 #config / routes.rb 
资源:个人资料,只有:[: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 | %GT;
<%@ attributes.each do | attr | %GT;
<%= f.text_field attr.to_sym%>
<%end%>
<%end%>

然后,您需要使用 after_sign_in_path 内容Kirti发布






已更新



以下是您将使用的迁移: p>

 #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
/ pre>

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

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.

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.

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

Thank you

解决方案

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

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]


Controller

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

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


Updated

Here is the migration you'd use:

# 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,Devise和个人资料页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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