gpt4 book ai didi

ruby-on-rails - 使用 Devise - Rails 4 保存具有嵌套形式的 has_one 模型

转载 作者:太空宇宙 更新时间:2023-11-03 16:43:45 24 4
gpt4 key购买 nike

当用户注册 Devise 时,我被困在一个试图保存 has_one 模型的 Rails 项目中。

我的应用正在保存子模型和新用户,但问题是它没有在用户表中保存子 ID。

我已经尝试了很多在 Stackoverflow 上找到的选项,但都没有成功。我做错了什么吗?

用户模型:

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:linkedin]

# Associations
has_one :build, inverse_of: :user
# Allow saving of attributes on associated records through the parent,
# :autosave option is automatically enabled on every association
accepts_nested_attributes_for :build, allow_destroy: true
end

构建模型

class Build < ActiveRecord::Base
belongs_to :user, inverse_of: :build
has_one :template
# validates_presence_of :user
end

注册 Controller

class Users::RegistrationsController < Devise::RegistrationsController
before_action :sign_up_params, only: [:create]
before_action :account_update_params, only: [:update]
# before_filter :configure_permitted_parameters

# GET /resource/sign_up
def new
# super

# Override Devise default behaviour and create a build as well
build_resource({})
resource.build_build
respond_with self.resource

# @build = @user.builds.build(template_id: params[:template_id], domain_url: params[:domain_url])
end

# POST /resource
def create
super
# @build = current_user.build.build(params[:post])

# @build = @user.build.build(template_id: sign_up_params[:template_id], domain_url: sign_up_params[:domain_url])
# @build.save
UserMailer.welcome_email(@user).deliver unless @user.invalid?
end

# GET /resource/edit
# def edit
# super
# end

# PUT /resource
# def update
# super
# end

# DELETE /resource
# def destroy
# super
# end

# GET /resource/cancel
# Forces the session data which is usually expired after sign
# in to be expired now. This is useful if the user wants to
# cancel oauth signing in/up in the middle of the process,
# removing all OAuth session data.
# def cancel
# super
# end

# protected
#
# def configure_permitted_parameters
# devise_parameter_sanitizer.for(:sign_up) { |u|
# u.permit(:first_name, :last_name, :email, :password, :password_confirmation, :builds_attributes => [:template_id, :domain_url])
# }
# end

# If you have extra params to permit, append them to the sanitizer.
# def configure_sign_up_params
# devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute])
# end

# If you have extra params to permit, append them to the sanitizer.
# def configure_account_update_params
# devise_parameter_sanitizer.permit(:account_update, keys: [:attribute])
# end

# The path used after sign up.
# def after_sign_up_path_for(resource)
# super(resource)
# end

# The path used after sign up for inactive accounts.
# def after_inactive_sign_up_path_for(resource)
# super(resource)
# end

private

def sign_up_params
devise_parameter_sanitizer.sanitize(:sign_up)
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, build_attributes: [:template_id, :domain_url])
end

def account_update_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password)
end
end

新用户表单 - 查看

<div class="container">
<h4 class="center-align">Sign up</h4>

<div id="signup-row" class="row z-depth-2">
<div id="signup" class="col s6">

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>

<div class="field">
<%= f.label :first_name %><br/>
<%= f.text_field :first_name, autofocus: true %>
</div>

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

<div class="field">
<%= f.label :email %><br/>
<%= f.email_field :email %>
</div>

<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %>
characters minimum)</em>
<% end %><br/>
<%= f.password_field :password, autocomplete: "off" %>
</div>

<div class="field">
<%= f.label :password_confirmation %><br/>
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>

<div class="actions">
<%= f.submit "Sign up", :class => 'btn black' %>
<%= render "devise/shared/links" %>

</div>

<%= f.fields_for :build do |o| %>
<%= o.hidden_field :template_id, value: params["template"] %>
<%= o.hidden_field :domain_url, value: params["domain"] %>
<% end %>
<% end %>
</div>

<div id="linkedin-signup" class="col s6">
<div class="center-align">
<h5>Sign up with LinkedIn </h5> <br>
<a href="http://localhost:3000/login/li/oauth2"><%= image_tag('linkedin.png', :class => "linkedIn-logo" ) %></a>
</div>
</div>
</div>


</div>

服务器/post日志

Started POST "/users" for 127.0.0.1 at 2016-08-03 13:31:22 +1000
Processing by Users::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"11DgWCU07EXLJKXbhWah0gREdBiN2PLkF/WxWsuqW5rgQdCUrRH9rLBrKpbusRhtsCzSAQT0ADlxhQxMvWAD6A==", "user"=>{"first_name"=>"john", "last_name"=>"maksksk", "email"=>"jofff@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "build_attributes"=>{"template_id"=>"1", "domain_url"=>"dddddd.com.au"}}, "commit"=>"Sign up"}
(0.1ms) BEGIN
User Exists (0.6ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'jofff@gmail.com' LIMIT 1
SQL (0.5ms) INSERT INTO "users" ("first_name", "last_name", "email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["first_name", "john"], ["last_name", "maksksk"], ["email", "jofff@gmail.com"], ["encrypted_password", "$2a$11$WgJJ.uM2DfaqQhUYatUZnuIJmaqIDVfIuYEkl/U3zSbm.h/OH/yGa"], ["created_at", "2016-08-03 03:31:22.796589"], ["updated_at", "2016-08-03 03:31:22.796589"]]
SQL (0.2ms) INSERT INTO "builds" ("template_id", "domain_url", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["template_id", 1], ["domain_url", "dddddd.com.au"], ["user_id", 30], ["created_at", "2016-08-03 03:31:22.800033"], ["updated_at", "2016-08-03 03:31:22.800033"]]
(1.3ms) COMMIT
(0.1ms) BEGIN
SQL (0.4ms) UPDATE "users" SET "last_sign_in_at" = $1, "current_sign_in_at" = $2, "last_sign_in_ip" = $3, "current_sign_in_ip" = $4, "sign_in_count" = $5, "updated_at" = $6 WHERE "users"."id" = $7 [["last_sign_in_at", "2016-08-03 03:31:22.804259"], ["current_sign_in_at", "2016-08-03 03:31:22.804259"], ["last_sign_in_ip", "127.0.0.1/32"], ["current_sign_in_ip", "127.0.0.1/32"], ["sign_in_count", 1], ["updated_at", "2016-08-03 03:31:22.805380"], ["id", 30]]
(1.2ms) COMMIT

感谢您的帮助!!

ps:我猜 build 不是一个聪明的表名...

托马斯

最佳答案

问题是用户先保存然后你为了关系目的保存构建你可以保存 user_id 来构建表而不是保存 build_id 给用户

关于ruby-on-rails - 使用 Devise - Rails 4 保存具有嵌套形式的 has_one 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38733933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com