2016-05-23 8 views
1

多くのフォームからhas_many :throughを実装しようとしていますが、データベースに問題が発生しました。私はフィールドのバリデーションを持っていません。それは、何よりもパラメータハッシュの構造について不平を言っています。Rails 5 ActiveRecord :: RecordInvalid(検証に失敗しました)、検証はありません

エラーは次のとおりです。

はActiveRecord :: RecordInvalid(検証は失敗しました:支出経費カテゴリ費用は存在している必要があります):

パラメータ:

は、パラメータのハッシュは、次のようになります: "" 2006年12月12日 "、"金額 "=>" 234 "、" check_number "=>" 234 "、"借方 " "=>" 0 "、" notes "=>" 234 "、" expense_expense_categories_attributes "=> {" 1464029611137 "=> { => "234"、 "expense_category_id" => "1"、 "_destroy" => "偽"}}} "量"、=> "費用を作成する" "コミット"}

私が気づくことの一つ接合テーブルに:expense_id値を追加していないということです。これはaccepts_nested_attributes_forメカニズムで行う必要がありますが、そうではありません。私はこれがRails 5の問題であると考え始めました。これは、類似した関係と同様に構造化されたフォームがうまく動作しているからです。私に行方不明のものが見えますか?

ここに私のコントローラです:

ここ
def create 

    @expense = Expense.new(expense_params) 
    respond_to do |format| 
    if @expense.save! 
    @expenses = Expense.all.paginate(:page => params[:page], :per_page => 9).order("created_at DESC") 

     format.html { redirect_to @expense, notice: 'Expense was successfully created.' } 
     format.js {} 
     format.json { render json: @expense, status: :created, location: @expense } 
    else 
     @expenses = Expense.all.paginate(:page => params[:page], :per_page => 9).order("created_at DESC") 
     format.html { render action: "index" } 
     format.js {} 
     format.json { render json: @expense.errors, status: :unprocessable_entity } 
    end 
    end 
end 

def expense_params 
    params.require(:expense).permit(:id, :date, :amount, :check_number, :debit, :notes, :amount, :expense_expense_categories_attributes => [:id, :amount, :expense_id , :expense_category_id, :_destroy]) 
end 

は私のモデルです:

費用

class Expense < ApplicationRecord 
    has_one :payee 
    monetize :amount_cents 
    has_many :expense_expense_categories 
    has_many :expense_categories, through: :expense_expense_categories, :dependent => :destroy 
    accepts_nested_attributes_for :expense_expense_categories,:allow_destroy => true 

end 

ExpenseCategory:

class ExpenseCategory < ApplicationRecord 
    has_many :expense_expense_categories 
    has_many :expenses, through: :expense_expense_categories 

end 

ExpenseExpenseCategory

class ExpenseExpenseCategory < ApplicationRecord 
    monetize :amount_cents 
    belongs_to :expense 
    belongs_to :expense_category 

    accepts_nested_attributes_for :expense_category 
end 

_form.html.erb:

<%= form_for @expense, html: { :class => "ui form segment" }, :remote => true do |f|%> 

    <div class="field"> 

     <%= f.label :date%> 
     <div class="ui small input"> 
     <%= f.date_field :date %> 
    </div> 
    </div> 
    <div class="field"> 
    <%= f.label :amount %> 
    <div class="ui small input"> 

     <%= f.text_field :amount %> 
    </div> 

    </div> 

    <div class="field"> 
    <%= f.label :check_number %> 
    <div class="ui small input"> 

     <%= f.text_field :check_number %> 
    </div> 

    </div> 

    <div class="field"> 
    <%= f.label :debit %> 
    <div class="ui small input"> 
     <%= f.check_box :debit %> 
    </div> 

    </div> 

    <div class="field"> 
    <%= f.label :notes %> 
    <div class="ui small input"> 
     <%= f.text_area :notes %> 
    </div> 

    </div> 

    <%= f.fields_for :expense_expense_categories do |builders| %> 
     <%= render 'expense_expense_category_fields', :f => builders %> 
    <% end %> 
    <%= link_to_add_fields "Add Category", f, :expense_expense_categories %> 

    <%= f.submit class: "ui blue button" %> 

expense_expense_category_fields.htnl.erbここ

<div class="field"> 
    <%=f.label :amount%> 
    <%= f.text_field :amount %> 
</div> 
<div class="field"> 
    <%=f.label :category%> 
    <%= f.select :expense_category_id, ExpenseCategory.all.collect { |p| [p.category, p.id] } %> 
</div> 
<%= f.hidden_field :_destroy %> 

<%= link_to "Remove option", "#", :class => "remove_expense_expense_categories" %> 

は、ブラウザからのフォームデータが提出されている:

utf8:✓ 
expense[date]:2016-05-12 
expense[amount]:23 
expense[check_number]:23 
expense[debit]:0 
expense[notes]: 
expense[expense_expense_categories_attributes][1464030986149][amount]:23 
expense[expense_expense_categories_attributes][1464030986149][expense_category_id]:1 
expense[expense_expense_categories_attributes][1464030986149][_destroy]:false 
expense[expense_expense_categories_attributes][1464030991099][amount]:43 
expense[expense_expense_categories_attributes][1464030991099][expense_category_id]:10 
expense[expense_expense_categories_attributes][1464030991099][_destroy]:false 
commit:Create Expense 

答えて

0

これは、費用が作成される前にRailsがExpenseExpenseCategoryを作成しようとしているためです。定義する必要があります。アソシエーション上の:inverse_ofそれは働いていた

class Expense < ApplicationRecord 
    has_one :payee 
    monetize :amount_cents 
    has_many :expense_expense_categories, inverse_of: :expense 
    has_many :expense_categories, through: :expense_expense_categories, :dependent => :destroy 
    accepts_nested_attributes_for :expense_expense_categories,:allow_destroy => true 

end 

class ExpenseCategory < ApplicationRecord 
    has_many :expense_expense_categories, inverse_of: :expense_category 
    has_many :expenses, through: :expense_expense_categories 

end 

class ExpenseExpenseCategory < ApplicationRecord 
    monetize :amount_cents 
    belongs_to :expense, inverse_of: :expense_expense_categories 
    belongs_to :expense_category, inverse_of: :expense_expense_categories 

    accepts_nested_attributes_for :expense_category 
end 

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#module-ActiveRecord::Associations::ClassMethods-label-Setting+Inverses

+0

。誰かがあなたに投票した理由は分かりません。ご協力ありがとうございました。 – ctilley79

+0

ただの簡単な質問です。私のレール4のアプリケーションでは 'inverse_of:'は必要ありませんでした。これはRails 5の新しい要件ですか? – ctilley79

+0

@ ctilley79私はそれが信じられません。親オブジェクトが新しいレコードの場合、inverse_ofが必要です。おそらく、Railsは可能なinverse_of値を作るためにクラス名をパラメータ化します。これは多くの場合正しいかもしれません。 – KPheasey

関連する問題