我一直在将我的rails应用程序从3.2.8升级到4.2.4并且我有大约98%的应用程序正在运行,但我遇到了与API有关的主要障碍。我继续得到一个奇怪的错误,我无法找到解决方案。
如果KnowledgeChecks中的所有代码>状态被注释掉了,我没有收到错误,但如果我有最轻微的代码,我会得到一个未定义的方法。此外,如果我在控制台中运行相同的代码(在API控制器之外),我不会收到任何错误。
这就是我目前所拥有的:
API应用程序控制器:
class Api::ApplicationController < ActionController::Metal
include AbstractController::Rendering
include ActionView::Layouts
include ActionController::MimeResponds
include ActionController::RespondWith
include AbstractController::Callbacks
include ActionController::Helpers
include ActionController::Head
# include ActiveModel::AttributeMethods
append_view_path "#{Rails.root}/app/views"
respond_to :js
skip_before_filter :set_guard
before_filter :require_api_conversation_token!
def require_api_conversation_token!
@user = User.where(:api_token => params[:api_token]).first
head :unauthorized unless @user
end
end
问题控制器(创建工作,状态失败)
class Api::KnowledgeChecksController < Api::ApplicationController
def create
@objective = Objective.find_by_id params[:id]
if @objective && @user.courses.include?(@objective.course)
KnowledgeCheck.create!(
:objective_id => @objective.id,
:user_id => @user.id,
:status => "in progress"
)
head :ok
else
head :unprocessable_entity
end
end
def status
kchk = @user.knowledge_checks.where(:objective_id => params[:id])
if kchk.length > 0 && params[:status] && params[:score]
kchk.last.status = params[:status]
kchk.last.score = params[:score]
kchk.last.save
head :ok
else
head :unprocessable_entity
end
end
end
日志中的结果:
Started POST "/api/knowledge_checks/status/?api_token=TOKEN&id=ID&status=STATUS&score=SCORE"
Unexpected error while processing request: undefined method `to_i' for true:TrueClass
/Users/ehay/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/content_length.rb:18:in `call'
/Users/ehay/.rvm/gems/ruby-2.2.3/gems/thin-1.6.4/lib/thin/connection.rb:86:in `block in pre_process'
/Users/ehay/.rvm/gems/ruby-2.2.3/gems/thin-1.6.4/lib/thin/connection.rb:84:in `catch'
/Users/ehay/.rvm/gems/ruby-2.2.3/gems/thin-1.6.4/lib/thin/connection.rb:84:in `pre_process'
/Users/ehay/.rvm/gems/ruby-2.2.3/gems/thin-1.6.4/lib/thin/connection.rb:53:in `process'
/Users/ehay/.rvm/gems/ruby-2.2.3/gems/thin-1.6.4/lib/thin/connection.rb:39:in `receive_data'
/Users/ehay/.rvm/gems/ruby-2.2.3/gems/eventmachine-1.0.8/lib/eventmachine.rb:193:in `run_machine'
/Users/ehay/.rvm/gems/ruby-2.2.3/gems/eventmachine-1.0.8/lib/eventmachine.rb:193:in `run'
/Users/ehay/.rvm/gems/ruby-2.2.3/gems/thin-1.6.4/lib/thin/backends/base.rb:73:in `start'
/Users/ehay/.rvm/gems/ruby-2.2.3/gems/thin-1.6.4/lib/thin/server.rb:162:in `start'
/Users/ehay/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/handler/thin.rb:19:in `run'
/Users/ehay/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/server.rb:286:in `start'
/Users/ehay/.rvm/gems/ruby-2.2.3/gems/railties-4.2.4/lib/rails/commands/server.rb:80:in `start'
/Users/ehay/.rvm/gems/ruby-2.2.3/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:80:in `block in server'
/Users/ehay/.rvm/gems/ruby-2.2.3/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:75:in `tap'
/Users/ehay/.rvm/gems/ruby-2.2.3/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:75:in `server'
/Users/ehay/.rvm/gems/ruby-2.2.3/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
/Users/ehay/.rvm/gems/ruby-2.2.3/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
非常感谢任何帮助。
答案 0 :(得分:0)
这真的很愚蠢。事后看来,我不确定为什么代码在Rails 3.2中有效。
基本上,&#34;状态&#34;是ActionController :: Metal的辅助方法。如果我在状态调用中添加了任何内容,则rails认为我正在尝试将其与帮助程序一起使用。更改控制器中方法的名称修复了我的问题。
http://api.rubyonrails.org/classes/ActionController/Metal.html