我是Rails的新手,并且正在建立一个工作板marketplace.I'm卡住了用户注册配置文件表单。我在Rails 5中使用了devise(4.2)。我在user_create之前创建了配置文件,然后只是重定向到配置文件视图,现在使用注册表单字段(如first_name & last_name)更新用户。Rails 5 - 在设计sign_up后清空用户配置文件
我的注册是一个双向过程,而不是嵌套的配置文件。 第1步 - 设计注册表格,只需要一个额外的字段来检查用户作为用户(求职者)或公司的角色。 第2步 - 基于此角色,为用户和公司创建单独的配置文件表单。如果配置文件未提交给数据库,则用户也不应保存,并且必须再次发生用户sign_up。
目前,我只用一个通用的用户表单,它具有first_name和last_name字段。我在用户模型中使用了before_create:build_profile,但它只是传递Profile.create而不在数据库上创建它。我应该在profiles_controller还是registrations_controller(设计)中覆盖'create'或'new'。
views/profiles/show.html.erb中的错误是:表单中的第一个参数不能包含零或为空。在终端上,我看到这个配置文件:“user_id”=>“2”,“id”=>“profile_id”} ...这是否表示DB中的用户和配置文件表之间的东西阻碍了创建或路由正确设置?
的routes.rb
Rails.application.routes.draw do
root "jobs#index"
devise_for :users, :controllers => {registrations: "registrations"}
resources :users do
resources :profiles, only: [:show, :update]
end
end
Registrations_controller
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(user)
if resource.class == User && current_user.role == 'user'
user_profile_path
else # to check for company user resource.class == User && current_user.role == 'company'
puts "redirecting to company profile-will do later"
end
end
end
profiles_controller.rb
class ProfilesController < ApplicationController
def show
end
def update
@profile = Profile.find_by_user_id(profile_params)
current_user.update(@profile)
end
def destroy
@profile.destroy
respond_with(@profile)
end
private
def profile_params
params.require(:user).permit(profile_attributes: [])
end
def set_profile
@profile = Profile.find(params[:id])
end
end
profile.rb
class Profile < ApplicationRecord
belongs_to :user
end
user.rb
class User < ApplicationRecord
before_create :build_profile
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
enum role: [:user, :company, :admin]
has_one :profile
def build_profile
Profile.create
true
end
感谢但实际上延伸到配置文件和用户公司是下一个过程现在配置文件没有被创建。 – Means