0
我有以下代碼(Slim格式),其中顯示一個表格users
。 user
的數據在提交時顯示並更新。但是,phone
這是來自publisher
的嵌套屬性,當我加載表單時顯示當前數據,但不會在提交時更新。設計nested_attributes字段顯示數據,但不更新模型
查看
= form_for current_user, url: user_registration_path, html: {class: 'account'} do |user_form|
.row
.control-group.text-center
label.col-9 Email
= user_form.text_field :email, class: 'col-7'
= fields_for :publisher, current_user.publisher do |publisher_form|
.row
.control-group.text-center
label.col-9 Phone
= publisher_form.text_field :phone, class: 'col-7'
註冊記憶控制器
class RegistrationsController < Devise::RegistrationsController
def update_resource(resource, params)
resource.update_without_password(params)
end
end
應用控制器
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:account_update) {|u| u.permit(
:first_name,
:last_name,
:email,
:password,
:password_confirmation,
:current_password,
publisher_attributes: [:phone, :payment_details],
banner_attributes: [:website, :banner_msg, :signup_msg, :bg_col, :txt_col, :btn_col]
)}
end
end
用戶模型
class User < ActiveRecord::Base
has_one :publisher
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
accepts_nested_attributes_for :publisher
validates_presence_of :first_name, :last_name
validates_length_of :first_name, maximum: 100
validates_length_of :last_name, maximum: 100
end
確保接受你的答案。這當然是正確的答案,它會給你一個奇特的新徽章! :d –