使用Rails 3.2.0.rc2和ruby 1.9.3p0
我有一个带有以下架构的报价表:
CREATE TABLE "quotes" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "quote_value" float, "quote_note" text, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
当我通过http://localhost:3000/quotes/new创建新报价时,我收到错误
wrong number of arguments (0 for 1)
和
app/controllers/quotes_controller.rb:47:in `block in create'
app/controllers/quotes_controller.rb:45:in `create'
以下内容来自development.log
Started POST "/quotes" for 127.0.0.1 at 2012-01-10 18:58:11 +0100
Processing by QuotesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"3l5KzYrwIOf84P3Y4JF1/iV1Sx+Uf4rfq3y6iUUinSw=", "quote"=>{"quote_value"=>"1", "quote_note"=>"hello"}, "commit"=>"Create Quote"}
quote is : #<Quote:0x324bbb8>
(0.1ms) begin transaction
SQL (5.8ms) INSERT INTO "quotes" ("created_at", "quote_note", "quote_value", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Tue, 10 Jan 2012 17:58:11 UTC +00:00], ["quote_note", "hello"], ["quote_value", "1"], ["updated_at", Tue, 10 Jan 2012 17:58:11 UTC +00:00]]
(0.5ms) rollback transaction
Completed 500 Internal Server Error in 8ms
ArgumentError (wrong number of arguments (0 for 1)):
app/controllers/quotes_controller.rb:47:in `block in create'
app/controllers/quotes_controller.rb:45:in `create'
为什么会发生这种情况?是否是因为插入查询中的格式化值?
由于
更新:quotes_controller.rb文件
class QuotesController < ApplicationController
# GET /quotes
# GET /quotes.json
def index
@quotes = Quote.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @quotes }
end
end
# GET /quotes/1
# GET /quotes/1.json
def show
@quote = Quote.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @quote }
end
end
# GET /quotes/new
# GET /quotes/new.json
def new
@quote = Quote.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @quote }
end
end
# GET /quotes/1/edit
def edit
@quote = Quote.find(params[:id])
end
# POST /quotes
# POST /quotes.json
def create
@quote = Quote.new(params[:quote])
respond_to do |format|
if @quote.save
format.html { redirect_to @quote, notice: 'Quote was successfully created.' }
format.json { render json: @quote, status: :created, location: @quote }
else
format.html { render action: "new" }
format.json { render json: @quote.errors, status: :unprocessable_entity }
end
end
end
# PUT /quotes/1
# PUT /quotes/1.json
def update
@quote = Quote.find(params[:id])
respond_to do |format|
if @quote.update_attributes(params[:quote])
format.html { redirect_to @quote, notice: 'Quote was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @quote.errors, status: :unprocessable_entity }
end
end
end
# DELETE /quotes/1
# DELETE /quotes/1.json
def destroy
@quote = Quote.find(params[:id])
@quote.destroy
respond_to do |format|
format.html { redirect_to quotes_url }
format.json { head :no_content }
end
end
end
更新2:添加模型
class Request < ActiveRecord::Base
validates :cust_email, :presence => true
has_many :quotes, :dependent => :destroy
has_many :brokers, :through => :quotes
end
class Broker < ActiveRecord::Base
has_many :quotes, :dependent => :destroy
has_many :requests, :through => :quotes
end
class Quote < ActiveRecord::Base
belongs_to :broker
belongs_to :request
end
更新3:以下是完整跟踪
vendor/bundle/gems/activerecord-3.2.0.rc2/lib/active_record/sanitization.rb:190:in `quote_value'
vendor/bundle/gems/activemodel-3.2.0.rc2/lib/active_model/dirty.rb:143:in `attribute_change'
vendor/bundle/gems/activemodel-3.2.0.rc2/lib/active_model/dirty.rb:117:in `block in changes'
vendor/bundle/gems/activemodel-3.2.0.rc2/lib/active_model/dirty.rb:117:in `map'
vendor/bundle/gems/activemodel-3.2.0.rc2/lib/active_model/dirty.rb:117:in `changes'
vendor/bundle/gems/activerecord-3.2.0.rc2/lib/active_record/attribute_methods/dirty.rb:23:in `save'
vendor/bundle/gems/activerecord-3.2.0.rc2/lib/active_record/transactions.rb:241:in `block (2 levels) in save'
vendor/bundle/gems/activerecord-3.2.0.rc2/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
vendor/bundle/gems/activerecord-3.2.0.rc2/lib/active_record/connection_adapters/abstract/database_statements.rb:190:in `transaction'
vendor/bundle/gems/activerecord-3.2.0.rc2/lib/active_record/transactions.rb:208:in `transaction'
vendor/bundle/gems/activerecord-3.2.0.rc2/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
vendor/bundle/gems/activerecord-3.2.0.rc2/lib/active_record/transactions.rb:241:in `block in save'
vendor/bundle/gems/activerecord-3.2.0.rc2/lib/active_record/transactions.rb:252:in `rollback_active_record_state!'
vendor/bundle/gems/activerecord-3.2.0.rc2/lib/active_record/transactions.rb:240:in `save'
app/controllers/quotes_controller.rb:50:in `block in create'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_controller/metal/mime_responds.rb:269:in `call'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_controller/metal/mime_responds.rb:269:in `retrieve_response_from_mimes'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_controller/metal/mime_responds.rb:194:in `respond_to'
app/controllers/quotes_controller.rb:45:in `create'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/abstract_controller/base.rb:167:in `process_action'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_controller/metal/rendering.rb:10:in `process_action'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
vendor/bundle/gems/activesupport-3.2.0.rc2/lib/active_support/callbacks.rb:414:in `_run__798983255__process_action__126235889__callbacks'
vendor/bundle/gems/activesupport-3.2.0.rc2/lib/active_support/callbacks.rb:405:in `__run_callback'
vendor/bundle/gems/activesupport-3.2.0.rc2/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
vendor/bundle/gems/activesupport-3.2.0.rc2/lib/active_support/callbacks.rb:81:in `run_callbacks'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/abstract_controller/callbacks.rb:17:in `process_action'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_controller/metal/rescue.rb:29:in `process_action'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
vendor/bundle/gems/activesupport-3.2.0.rc2/lib/active_support/notifications.rb:119:in `block in instrument'
vendor/bundle/gems/activesupport-3.2.0.rc2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
vendor/bundle/gems/activesupport-3.2.0.rc2/lib/active_support/notifications.rb:119:in `instrument'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_controller/metal/params_wrapper.rb:205:in `process_action'
vendor/bundle/gems/activerecord-3.2.0.rc2/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/abstract_controller/base.rb:121:in `process'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/abstract_controller/rendering.rb:45:in `process'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_controller/metal.rb:199:in `dispatch'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_controller/metal.rb:242:in `block in action'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_dispatch/routing/route_set.rb:66:in `call'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_dispatch/routing/route_set.rb:66:in `dispatch'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_dispatch/routing/route_set.rb:30:in `call'
vendor/bundle/gems/journey-1.0.0.rc4/lib/journey/router.rb:60:in `block in call'
vendor/bundle/gems/journey-1.0.0.rc4/lib/journey/router.rb:48:in `each'
vendor/bundle/gems/journey-1.0.0.rc4/lib/journey/router.rb:48:in `call'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_dispatch/routing/route_set.rb:570:in `call'
vendor/bundle/bundler/gems/omniauth-966a4653a5fd/lib/omniauth/strategy.rb:168:in `call!'
vendor/bundle/bundler/gems/omniauth-966a4653a5fd/lib/omniauth/strategy.rb:148:in `call'
vendor/bundle/bundler/gems/omniauth-966a4653a5fd/lib/omniauth/builder.rb:28:in `call'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
vendor/bundle/gems/rack-1.4.0/lib/rack/etag.rb:23:in `call'
vendor/bundle/gems/rack-1.4.0/lib/rack/conditionalget.rb:35:in `call'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_dispatch/middleware/head.rb:14:in `call'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_dispatch/middleware/params_parser.rb:21:in `call'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_dispatch/middleware/flash.rb:242:in `call'
vendor/bundle/gems/rack-1.4.0/lib/rack/session/abstract/id.rb:205:in `context'
vendor/bundle/gems/rack-1.4.0/lib/rack/session/abstract/id.rb:200:in `call'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_dispatch/middleware/cookies.rb:338:in `call'
vendor/bundle/gems/activerecord-3.2.0.rc2/lib/active_record/query_cache.rb:64:in `call'
vendor/bundle/gems/activerecord-3.2.0.rc2/lib/active_record/connection_adapters/abstract/connection_pool.rb:443:in `call'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
vendor/bundle/gems/activesupport-3.2.0.rc2/lib/active_support/callbacks.rb:405:in `_run__429521786__call__490392356__callbacks'
vendor/bundle/gems/activesupport-3.2.0.rc2/lib/active_support/callbacks.rb:405:in `__run_callback'
vendor/bundle/gems/activesupport-3.2.0.rc2/lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
vendor/bundle/gems/activesupport-3.2.0.rc2/lib/active_support/callbacks.rb:81:in `run_callbacks'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_dispatch/middleware/reloader.rb:63:in `call'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
vendor/bundle/gems/railties-3.2.0.rc2/lib/rails/rack/logger.rb:26:in `call_app'
vendor/bundle/gems/railties-3.2.0.rc2/lib/rails/rack/logger.rb:16:in `call'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_dispatch/middleware/request_id.rb:22:in `call'
vendor/bundle/gems/rack-1.4.0/lib/rack/methodoverride.rb:21:in `call'
vendor/bundle/gems/rack-1.4.0/lib/rack/runtime.rb:17:in `call'
vendor/bundle/gems/activesupport-3.2.0.rc2/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
vendor/bundle/gems/rack-1.4.0/lib/rack/lock.rb:15:in `call'
vendor/bundle/gems/actionpack-3.2.0.rc2/lib/action_dispatch/middleware/static.rb:53:in `call'
vendor/bundle/gems/railties-3.2.0.rc2/lib/rails/engine.rb:479:in `call'
vendor/bundle/gems/rack-1.4.0/lib/rack/content_length.rb:14:in `call'
vendor/bundle/gems/railties-3.2.0.rc2/lib/rails/rack/log_tailer.rb:14:in `call'
vendor/bundle/gems/rack-1.4.0/lib/rack/handler/webrick.rb:59:in `service'
/Users/username/.rbenv/versions/1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/Users/username/.rbenv/versions/1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/Users/username/.rbenv/versions/1.9.3-p0/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
答案 0 :(得分:0)
好吧,如果Quote
是Broker
和Request
的加入模型,那么它应该具有broker_id
和request_id
属性以及数据库中的相应列 - { {3}}
根据您提供给我们的架构,它还不是连接表。 您应该在请求中指定上述参数。
答案 1 :(得分:0)
有一种名为'quote'的activerecord方法可能会产生干扰。您可以通过从命令行执行bundle open activerecord
并查看第190行的lib / sanitization.rb文件来查看activerecord。我怀疑您的'quote'方法和activerecord方法相互冲突。
以下是rails中保留字的列表。 “报价不能用作列名”
http://latheesh.com/2010/02/02/rails-reserved-words/
如果是这样的话,重命名你的课将解决这个问题。