2016-09-12 5 views
0

では動作しません作成し、私は私のofficers\_form.html.erbビュー内の一部のコマンドをレンダリング挿入した:RailsはボタンがDRY原則以下の部分的レンダリング

<%= form_for(user) do |f| %> 
    <% if user.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2> 

     <ul> 
     <% user.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <%= f.fields_for :user do |user_fields| %> 
    <div class="field"> 
     <%= user_fields.label :last_name %> 
     <%= user_fields.text_field :last_name %> 
    </div> 

    <div class="field"> 
     <%= user_fields.label :first_name %> 
     <%= user_fields.text_field :first_name %> 
    </div> 

    <div class="field"> 
     <%= user_fields.label :middle_name %> 
     <%= user_fields.text_field :middle_name %> 
    </div> 

    <div class="field"> 
     <%= user_fields.label :gender %> 
     <%= user_fields.select(:gender, User.genders.keys) %> 
    </div> 
    <% end %> 

    <!--div class="actions"--> 
    <!--%= f.submit %--> 
    <!--/div--> 
<% end %> 

<%= form_for(officer) do |f| %> 
    <% if officer.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(officer.errors.count, "error") %> prohibited this officer from being saved:</h2> 

     <ul> 
     <% officer.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <%= render :partial => 'users/form', :locals => {:user => @officer.user} %> 
    <%= render :partial => 'addresses/form', :locals => {:address => @officer.address} %> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

これは私のusers\_form.html.erbファイルですユーザーコードと同じ理由がAddressesコードに適用されるので、ここでは省略します。

これは私の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

、これを行いますあなたのアドレス属性に私はあなたのデータベースフィールドを知っていないので、それはあなたのためにその部分を行うことはできませんが、user_attributesとほとんど同じですが、異なるフィールドを除いて(idと:_destroyはすべて同じです)。

EDIT:1が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' %> 

これは、ネストされた形です。

何を持っていることはこれです:

<%= 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' %> 

今、あなたは、ネストされたフォームを持っていない、あなただけの3種類の完全形を持っていて、ボタンをこのように提出1と、複数のフォームを提出することはできません。

+0

あなたの提案は反映されていますが、運は反映されません。 – Zanzi

+0

Btw、私の現在のコードではすべてを許可しているので、最初の提案は必須ではありません。提案... – Zanzi

+0

どのようなエラーが表示されていますか、具体的には動作していないものはありますか?他のコントローラーとモデルを表示してください。また、ネストされたフォームのパーシャルをレンダリングする必要はありません。 f.fields_forの部分だけを使うことができます。 – luissimo

関連する問題