当我从我的js文件发送发布请求时

$http.post('/users/:user_id/tasks/', {id: $scope.task.id, title: data});


到控制器中的create动作时,出现以下错误。

javascript - #<任务:0x007f91f6442390>的未定义方法`upcase'-LMLPHP

带参数

javascript - #<任务:0x007f91f6442390>的未定义方法`upcase'-LMLPHP

这是什么意思?

控制者

  def create
    respond_to current_user.tasks.create(task_params)
  end

  private

  def task_params
    params.require(:task).permit(:id, :title, :due_date, :priority, :complete)
  end


耙路

         user_tasks GET    /users/:user_id/tasks(.:format)          tasks#index
                    POST   /users/:user_id/tasks(.:format)          tasks#create
      new_user_task GET    /users/:user_id/tasks/new(.:format)      tasks#new
     edit_user_task GET    /users/:user_id/tasks/:id/edit(.:format) tasks#edit
          user_task GET    /users/:user_id/tasks/:id(.:format)      tasks#show
                    PATCH  /users/:user_id/tasks/:id(.:format)      tasks#update
                    PUT    /users/:user_id/tasks/:id(.:format)      tasks#update
                    DELETE /users/:user_id/tasks/:id(.:format)      tasks#destroy


Rails控制台日志

Started POST "/users/:user_id/tasks/" for 127.0.0.1 at 2015-08-24 15:46:23 +0300
Processing by TasksController#create as HTML
  Parameters: {"id"=>1, "title"=>"some text", "user_id"=>":user_id", "task"=>{"id"=>1, "title"=>"some text"}}
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
   (0.2ms)  begin transaction
   (0.1ms)  commit transaction
Completed 500 Internal Server Error in 11ms (ActiveRecord: 0.8ms)

NoMethodError (undefined method `upcase' for #<Task:0x007f91f513a4c8>):
  app/controllers/tasks_controller.rb:22:in `create'


  Rendered /home/alex/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_source.erb (9.9ms)
  Rendered /home/alex/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (4.4ms)
  Rendered /home/alex/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.6ms)
  Rendered /home/alex/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (39.4ms)
Cannot render console with content type application/jsonAllowed content types: [#<Mime::Type:0x00000003c77a60 @synonyms=["application/xhtml+xml"], @symbol=:html, @string="text/html">, #<Mime::Type:0x00000003c57cb0 @synonyms=[], @symbol=:text, @string="text/plain">, #<Mime::Type:0x00000003c4b960 @synonyms=[], @symbol=:url_encoded_form, @string="application/x-www-form-urlencoded">]

最佳答案

我以前没看过这种语法

def create
  respond_to current_user.tasks.create(task_params)
end


通常是这样的

def create
  if current_user.tasks.create(task_params)
    @created = true
  end
  respond_to do |format|
    format.html #will do default which is render :action => "create"
  end
end


我认为这可能是您的问题:response_to被传递了一个意料之外的Task对象,我猜对传递的内容调用upcase是其正常行为的一部分。

编辑
也许是respond_with而不是respond_to吗?

http://apidock.com/rails/ActionController/MimeResponds/respond_with

关于javascript - #<任务:0x007f91f6442390>的未定义方法`upcase',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32182842/

10-11 05:40