我試圖實現一個has_many :through
多對多的形式,但我有一個問題提交給數據庫。我沒有字段驗證,它告訴我它抱怨哈希參數的結構比什麼都重要。Rails 5 ActiveRecord :: RecordInvalid(驗證失敗),但我沒有驗證
的錯誤是:
的ActiveRecord :: RecordInvalid(驗證失敗:費用支出類別費用必須存在):
參數哈希是這樣的:
參數:{「utf8」=>「✓」,「費用」=> {「date」=>「2006/12/12」,「amount」=>「234」,「check_number」=>「234」 「=>」0「,」notes「=>」234「,」expense_expense_categories_attributes「=> {」1464029611137「=> { 「量」=> 「234」, 「expense_category_id」=> 「1」, 「_destroy」=> 「假」}}}, 「提交」=> 「創建費用」}
一件事我注意到它不會將:expense_id
值添加到聯結表中。這應該通過accept_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
它的工作。我不確定爲什麼有人下了你的票。感謝您的幫助。 – ctilley79
只是一個簡單的問題。在我的rails 4應用程序中'inverse_of:'不是必需的。這是Rails 5的新要求嗎? – ctilley79
@ ctilley79我不相信。如果父對象是新記錄,則需要inverse_of。也許Rails參數化類名來創建一個可能的inverse_of值,在許多情況下這可能是正確的。 – KPheasey