stackoverflow の最初の投稿です。noob のようなものについては事前にお詫びします。
機能テストの 1 つでエラーが発生しましたが、何時間も経った後、エラーをパスする方法がわかりません。
要するに、invoice_schedule
which has_many
invoice_milestones
. 次に、invoice_milestones
属性estimate_percentage
(整数) を持ちます。私のモデルには、すべての合計が100 でなければならないmodels/invoice_schedule.rb
という検証があります。estimate_percentages
create アクションの機能テストはinvoice_schedule
毎回失敗します。以下の現在のコードでは、代わりにエラーが発生しています。渡したパラメーターでエラーが発生する方法がわかりません。
ということも記しておきますestimate has_one :invoice_schedule
どんな助けや提案も大歓迎です:)
models/invoice_schedule.rb:
class InvoiceSchedule < ActiveRecord::Base
belongs_to :estimate
has_many :invoice_milestones, :dependent => :destroy
accepts_nested_attributes_for :invoice_milestones, :allow_destroy => true
validate :total_must_equal_one_hundred_percent
def total_must_equal_one_hundred_percent
if total_percent != 100
errors.add(:invoice_milestones, "Total must equal 100%")
end
end
def total_percent
invoice_milestones.to_a.sum { |item| item.estimate_percentage || 0 }
end
end
models/invoice_milestone.rb:
class InvoiceMilestone < ActiveRecord::Base
belongs_to :invoice_schedule
belongs_to :invoice
validates :estimate_percentage, :numericality => {:only_integer => true, :greater_than_or_equal_to => 0, :less_than_or_equal_to => 100}
end
controllers/invoice_schedules_controller.rb
def create
@invoice_schedule = InvoiceSchedule.new(params[:invoice_schedule])
@estimate = Estimate.find_by_id(params[:estimate_id])
respond_to do |format|
if @invoice_schedule.save
format.html { redirect_to invoice_schedule_path(@invoice_schedule), :flash => {:notice => 'Invoice schedule was successfully created.', :status => 'success'} }
format.json { render json: @invoice_schedule, status: :created, location: @invoice_schedule }
else
format.html { render action: "new" }
format.json { render json: @invoice_schedule.errors, status: :unprocessable_entity }
end
end
end
テスト/機能/invoice_schedules_controller_test.rb
setup do
@account = accounts(:lorem)
@estimate = estimates(:lorem_one)
@user = users(:lorem_vendor)
@request.host = "#{@account.subdomain}.myapp.local"
session[:user_id] = @user.id
@invoice_schedule = invoice_schedules(:lorem_one)
@invoice_milestone = invoice_milestones(:lorem_one)
@data_estimate = estimates(:lorem_two)
@data_invoice_schedule = @invoice_schedule.attributes.merge({estimate_id: @data_estimate.id})
end
test "should create invoice_schedule" do
assert_difference('InvoiceSchedule.count') do
post :create, :invoice_schedule => { estimate_id: @data_estimate, :invoice_milestone => {estimate_percentage: 100}}
end
assert_redirected_to estimate_invoice_schedule_path(@estimate, assigns(:invoice_schedule))
end
config/routes.rb
require 'subdomain'
Accountimize::Application.routes.draw do
resources :invoices do
member do
get 'generateInvoiceFromMilestone'
end
end
resources :users
resources :sessions
get "sign_up" => "accounts#new", :as => "sign_up"
resources :accounts
resources :clients do
get :client_address, on: :member
end
resources :estimates do
resources :invoice_schedules, :shallow => true
end
resources :line_items
constraints(Subdomain) do
match '/' => 'accounts#show'
get "log_in" => "sessions#new", :as => "log_in"
get "log_out" => "sessions#destroy", :as => "log_out"
get "register" => "users#new", :as => "register"
end
root :to => 'site#index', :as => 'site'
end
私が得るエラーのトレース:
InvoiceSchedulesControllerTest
test_should_create_invoice_schedule ERROR
No route matches {:invoice_schedule=>{:estimate_id=>"706507935", :invoice_milestones_attributes=>{"0"=>{:description=>"test", :estimate_percentage=>"100"}}}, :controller=>"invoice_schedules", :action=>"create"}
STDERR:
Exception `ActionController::RoutingError' at /Users/Matt/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/actionpack-3.1.1/lib/action_dispatch/routing/route_set.rb:465:in `raise_routing_error'
/Users/Matt/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/actionpack-3.1.1/lib/action_dispatch/routing/route_set.rb:461:in `rescue in generate'
/Users/Matt/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/actionpack-3.1.1/lib/action_dispatch/routing/route_set.rb:453:in `generate'
/Users/Matt/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/actionpack-3.1.1/lib/action_dispatch/routing/route_set.rb:494:in `generate'
/Users/Matt/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/actionpack-3.1.1/lib/action_dispatch/routing/route_set.rb:490:in `generate_extras'
/Users/Matt/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/actionpack-3.1.1/lib/action_dispatch/routing/route_set.rb:486:in `extra_keys'
/Users/Matt/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/actionpack-3.1.1/lib/action_controller/test_case.rb:145:in `assign_parameters'
/Users/Matt/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/actionpack-3.1.1/lib/action_controller/test_case.rb:438:in `process'
/Users/Matt/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/actionpack-3.1.1/lib/action_controller/test_case.rb:49:in `process'
/Users/Matt/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/actionpack-3.1.1/lib/action_controller/test_case.rb:370:in `post'
test/functional/invoice_schedules_controller_test.rb:35:in `block (2 levels) in <class:InvoiceSchedulesControllerTest>'
/Users/Matt/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.1.1/lib/active_support/testing/assertions.rb:55:in `assert_difference'
test/functional/invoice_schedules_controller_test.rb:30:in `block in <class:InvoiceSchedulesControllerTest>'
/Users/Matt/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.1.1/lib/active_support/testing/setup_and_teardown.rb:35:in `block in run'
/Users/Matt/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.1.1/lib/active_support/callbacks.rb:444:in `_run_setup_callbacks'
/Users/Matt/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.1.1/lib/active_support/callbacks.rb:81:in `run_callbacks'
/Users/Matt/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.1.1/lib/active_support/testing/setup_and_teardown.rb:34:in `run'
機能テストで新しく作成されたinvoice_scheduleにinvoice_milestoneを追加するために、他にもいくつかの方法を試しましたが、うまくいかなかったようです。accept_nested_attributes_for が追加されている場合、上記のルーティング エラーが発生するのはなぜですか?