렌더링과 함께 작동하지 않습니다 생성, 나는 내 officers\_form.html.erb
보기 내에서 부분적인 명령을 렌더링 삽입 한 User 코드와 동일한 이유가 Addresses 코드에 적용되므로 여기서는 생략 할 것입니다.레일이 버튼은 DRY 규칙에 따라 부분
이 내 officers_controller
파일 : 나는 http://localhost:3000/officers/new
에 가면
class OfficersController < BaseController
before_action :set_officer, only: [:show, :edit, :update, :destroy]
# GET /officers
# GET /officers.json
def index
@officers = Officer.all
end
# GET /officers/1
# GET /officers/1.json
def show
end
# GET /officers/new
def new
@officer = Officer.new
end
# GET /officers/1/edit
def edit
end
# POST /officers
# POST /officers.json
def create
@officer = Officer.new(officer_params)
respond_to do |format|
if @officer.save
format.html { redirect_to @officer, notice: 'Officer was successfully created.' }
format.json { render :show, status: :created, location: @officer }
else
format.html { render :new }
format.json { render json: @officer.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /officers/1
# PATCH/PUT /officers/1.json
def update
respond_to do |format|
if @officer.update(officer_params)
format.html { redirect_to @officer, notice: 'Officer was successfully updated.' }
format.json { render :show, status: :ok, location: @officer }
else
format.html { render :edit }
format.json { render json: @officer.errors, status: :unprocessable_entity }
end
end
end
# DELETE /officers/1
# DELETE /officers/1.json
def destroy
@officer.destroy
respond_to do |format|
format.html { redirect_to officers_url, notice: 'Officer was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_officer
@officer = Officer.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def officer_params
#params.fetch(:officer, {})
params.require(:officer).permit!
end
end
지금, 부품은 사용자와 주소의 형태가 모두 표시됩니다에 포함,하지만 난 누를 때 Create officer
버튼 아무 일도 일어나지 않습니다. 오류는 어디에 있습니까?
def officer_params
#params.fetch(:officer, {})
params.require(:officer).permit(:id, user_attributes: [:id, :last_name, :middle_name, :first_name, :gender, :_destroy])
end
을 또한
'accepts_nested_attributes_for :user, reject_if: :all_blank, allow_destroy: true
accepts_nested_attributes_for :address, reject_if: :all_blank, allow_destroy: true'
에 accepts_nested_attributes_for :user, :address
을 변경하고 다음이 필요합니다
class Officer < ApplicationRecord
belongs_to :manager#, inverse_of: :officer
has_many :customers#, inverse_of: :officer
has_one :user, as: :userable, dependent: :destroy
has_one :address, as: :addressable, dependent: :destroy
accepts_nested_attributes_for :user, :address
end
class Manager < ApplicationRecord
has_many :officers#, inverse_of: :manager
has_one :user, as: :userable, dependent: :destroy
has_one :address, as: :addressable, dependent: :destroy
accepts_nested_attributes_for :user, :address
end
class User < ApplicationRecord
enum gender: { female: 0, male: 1, undefined: 2 }
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :userable, polymorphic: true
end
감사합니다, 당신은 당신의 officer_params에 user_attributes을 설정하지 않은 FZ
내 제안을 반영했지만 운이 없었습니다. – Zanzi
Btw, 첫 번째 제안은 필수 사항이 아니어야합니다. 현재 코드에서는 모든 것을 허용하고 있기 때문에 두 번째 목표는 모르지만 제안 ... – Zanzi
어떤 종류의 오류가 발생 했습니까? 아니면 특별히 작동하지 않는 오류가 있습니까? 다른 컨트롤러와 모델을 보여주세요. 또한 중첩 된 폼에 부분을 렌더링 할 필요가 없습니다. f.fields_for 부분 만 사용할 수 있습니다. – luissimo