首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails 4,页面

Rails 4,页面
EN

Stack Overflow用户
提问于 2014-02-23 23:12:15
回答 2查看 3.7K关注 0票数 4

我对编码很陌生,所以这可能是一个简单的问题。

大约一个月前,我开始使用RoR。不幸的是,我撞到了一个颠簸,似乎无法克服它。我试着寻找其他的问题来寻求帮助,但我仍然是新手,所以编码建议对我来说还是有点陌生。我希望有人能把事情说得更友好些。

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

在过去的3天里,我一直在努力弄清楚如何为每个新用户创建一个配置文件页面。我看了github自述文件,但我还是很困惑。

我已经生成了一个用户控制器和用户视图,但我甚至不知道是否需要执行这些步骤,因为我已经设计了这些步骤。如果你们能给我什么帮助,我会很感激的。

这里有一个指向我的github页面的链接- https://github.com/Thefoodie/PupPics

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-24 10:06:01

至于Kirti的答案,您实际上需要有一个可以重定向到的profile

模型

代码语言:javascript
复制
#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

模式

代码语言:javascript
复制
profiles
id | user_id | name | birthday | other | info | created_at | updated_at

路由

代码语言:javascript
复制
#config/routes.rb
resources :profiles, only: [:edit]

控制器

代码语言:javascript
复制
#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

视图

代码语言:javascript
复制
#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信息

更新的

下面是您将使用的迁移:

代码语言:javascript
复制
# 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
票数 10
EN

Stack Overflow用户

发布于 2014-02-23 23:26:43

首先,您需要在after_sign_in_path_for中为您的资源设置ApplicationControllerafter_sign_up_path_for,这将指向profile页面。然后您需要创建一个controller,它将呈现profile页面。

例如:(根据您的需求进行更改)

ApplicationController中定义路径

代码语言:javascript
复制
  def after_sign_in_path_for(resource)
    profile_path(resource)
  end

  def after_sign_up_path_for(resource)
    profile_path(resource)
  end

ProfilesController

代码语言:javascript
复制
  ## 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

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21976002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档