Rails:没有方法错误(未定义方法`create')

时间:2014-05-08 17:54:40

标签: ios ruby-on-rails ruby

我使用Doorkeeper在我的rails应用程序中设置了oauth。我能够从我的客户端iOS应用程序登录和检索访问令牌。当我尝试发布数据时,我得到了无方法错误。

这是我的API代码:

module Api
module V1
    class RolesController < ActionController::Base
    doorkeeper_for :all
    respond_to :json

    def create
        respond_with current_user.create(ob_params)
    end

    def index
        respond_with current_user
    end

def ob_params
  params.require(:role).permit(:submit_by, :gender, :short_desc, :long_desc, :title)
end

private

    def current_user
        @current_user ||= Boss.find(doorkeeper_token.resource_owner_id)
    end

    end
end
end

日志显示:

      [1m[35mBoss Load (0.3ms)[0m  SELECT "bosses".* FROM "bosses" WHERE "bosses"."id" = $1 LIMIT 1  [["id", 2]]
Completed 500 Internal Server Error in 21ms

NoMethodError (undefined method `create' for #<Boss:0x007fe1d037a410>):
  app/controllers/api/v1/roles_controller.rb:9:in `create'

1 个答案:

答案 0 :(得分:1)

不遵循你的逻辑。你不能创建已经存在的东西,它看起来像current_user。我希望:

def create
    respond_with Boss.create(ob_params)
end

和/或

def update
    respond_with current_user.update(ob_params)
end

编辑。怎么样:

def create
    @role = Role.create(ob_params)
    current_user.roles << @role
    @role.save #Needed?
    current_user.save #Needed?
    respond_with @role
end