2016-09-12 3 views
0

렌더링과 함께 작동하지 않습니다 생성, 나는 내 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

답변

1

, 이렇게 ~에 대한 주소 _ 속성 ficer params뿐만 아니라 당신의 데이터베이스 필드를 알지 못하기 때문에 그 부분을 할 수는 없지만 user_attributes와 같지만 다른 필드는 있습니다 (ID와 : _destroy는 모두 동일합니다).

편집 : 하나 aswell 모든 중첩 된 형태의 버튼 공급을 제출

<%= form_for(officer) do |f %> 

<%= f.fields_for :user do |user| %> 
    <%= user.text_field :last_name %> 
    <%= user.text_field :middle_name %> 
    <%= user.text_field :first_name %> 
<% end %> 

<%= f.fields_for :address do |address| %> 
    <%= address.text_field :street_name %> 
    <%= address.text_field :zip_code %> 
<% end %> 

<%= f.submit 'submit' %> 

이 방법 :

이 중첩 된 형태입니다. , 당신은 단지 3 개 개의 전체 형태를 한이 방법 제출 '버튼을 사용하면 여러 형태를 제출할 수 없습니다 이제

<%= form_for(officer) do |f %> 
<%= form_for(user) do |f| 
    <%= f.fields_for :user do |user| %> // this (f) now stands for the user form instead of the officer form 
    <%= user.text_field :last_name %> 
    <%= user.text_field :middle_name %> 
    <%= user.text_field :first_name %> 
    <% end %> 
<% end %> 

<%= form_for(address) do |f| %> 
<%= f.fields_for :address do |address| %> // same for this one 
    <%= address.text_field :street_name %> 
    <%= address.text_field :zip_code %> 
<% end %> 
<% end %> 

<%= f.submit 'submit' %> 

중첩 된 형태가없는 :

은 무엇 당신이 가진 것은 이것이다 .

+0

내 제안을 반영했지만 운이 없었습니다. – Zanzi

+0

Btw, 첫 번째 제안은 필수 사항이 아니어야합니다. 현재 코드에서는 모든 것을 허용하고 있기 때문에 두 번째 목표는 모르지만 제안 ... – Zanzi

+0

어떤 종류의 오류가 발생 했습니까? 아니면 특별히 작동하지 않는 오류가 있습니까? 다른 컨트롤러와 모델을 보여주세요. 또한 중첩 된 폼에 부분을 렌더링 할 필요가 없습니다. f.fields_for 부분 만 사용할 수 있습니다. – luissimo