Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RubyonRails4、Desive和profile页面_Ruby On Rails_Ruby On Rails 4_Devise_User Profile - Fatal编程技术网

Ruby on rails RubyonRails4、Desive和profile页面

Ruby on rails RubyonRails4、Desive和profile页面,ruby-on-rails,ruby-on-rails-4,devise,user-profile,Ruby On Rails,Ruby On Rails 4,Devise,User Profile,我不熟悉编码,所以这可能是一个简单的问题 我大约一个月前开始使用RoR。不幸的是,我碰到了一个障碍,似乎无法克服。我试着看其他的SO问题寻求帮助,但我还是一个新手,所以编码建议对我来说还是有点陌生。我希望有人能用更友好的新手术语来表达 我想做的是让我的网站为每个注册用户设置一个个人资料。这将是一个只有用户和管理员才能访问的私有配置文件。在用户注册/登录后,我希望他们被重定向到他们的个人资料,在那里他们可以编辑年龄和体重等信息 在过去的3天里,我一直在想如何为每个新用户创建一个配置文件页面。我查看

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

我大约一个月前开始使用RoR。不幸的是,我碰到了一个障碍,似乎无法克服。我试着看其他的SO问题寻求帮助,但我还是一个新手,所以编码建议对我来说还是有点陌生。我希望有人能用更友好的新手术语来表达

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

在过去的3天里,我一直在想如何为每个新用户创建一个配置文件页面。我查看了Desive github自述文件,但仍然感到困惑

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

这是我的github页面的链接-


感谢您

首先,您需要在中的登录后设置,并在应用程序控制器中为您的资源设置登录后设置,这将直接指向
配置文件
页面。 然后您需要创建一个
控制器
,它将呈现
配置文件
页面

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

ApplicationController
中定义路径

  def after_sign_in_path_for(resource)
    profile_path(resource)
  end

  def after_sign_up_path_for(resource)
    profile_path(resource)
  end
档案控制器中

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

此外,请确保为用户配置文件创建一个
视图。

进一步了解Kirti的答案,您将需要一个
配置文件来重定向到:

型号

#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
#config/routes.rb
resources :profiles, only: [:edit]

路线

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

太棒了,我实际上创建了一个用户配置文件脚手架,它处理了大部分内容。我希望我能对你和科蒂的答案都投赞成票,但我还没有代表权。该网站正在将用户重定向到他们自己的个人资料页面,现在我需要弄清楚如何将他们自己的个人信息放在该页面上。感谢您的反馈-希望有帮助!你可以使用我的代码来显示你需要的数据!如何将该模式添加到数据库?更新的答案谢谢您的帮助!当我获得更多的声誉时,我一定会回来投票给这个答案。