繁体   English   中英

为设计用户RoR创建配置文件页面

[英]Creating profile page for devise user RoR

我是RoR的新手,并且正在为这个基本项目要求而苦苦挣扎。

我正在使用Devise进行用户身份验证,效果很好,并且在Devise中注册用户帐户后,我在注册控制器中使用了自定义重定向到新页面,用户可以在其中创建其个人资料。 注册后,我不断收到以下错误:

“格式中的第一个参数不能包含nil或为空”。 有人可以指出我正确的方向吗? 提前致谢!

代码如下:

注册管理员:

class RegistrationsController < Devise::RegistrationsController
  protected

  def after_sign_up_path_for(resource)
    new_path(resource)
  end
end

创建新的个人资料页面:

<%= form_for @profile do |f| %>

<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>

<div class="actions">
<%= f.submit %>
</div>

<% end %>

配置文件控制器:

class ProfileController < ApplicationController

def new
 @profiles = current_user.build_profile
end

def create
 @profiles = current_user.build_profile(params[:profile].permit( :name))
end 

end

轮廓模型:

class Profile < ActiveRecord::Base

belongs_to :user

end

用户模型:

class User < ActiveRecord::Base

devise :database_authenticatable, :registerable,
        :recoverable, :rememberable, :trackable, :validatable

validates :name, presence: true

has_one :profile

end

数据库架构文件:

ActiveRecord::Schema.define(version: 20131124211354) do

  create_table "profiles", force: true do |t|
    t.integer  "user_id"
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", force: true do |t|
    t.string   "name"
    t.string   "location"
    t.string   "email",                  default: "", null: false
    t.string   "phone"
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

在您的@profiles = current_user.build_profile ,您分配@profiles = current_user.build_profile ,但是在您的表单中,您引用form_for @profile do |f| 注意多元化的区别。 在控制器中将其更改为@profile,您应该可以进行...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM