简体   繁体   English

Rails4-如何在编写API时从RocketPants :: Base继承?

[英]Rails4 - How to inherit from RocketPants::Base while writing API?

I am trying to create API for my Rails app. 我正在尝试为我的Rails应用程序创建API。 I have these following controllers. 我有以下这些控制器。 在此处输入图片说明

I have the following inheritance 我有以下继承

/app/controllers/api/api_controller.rb /app/controllers/api/api_controller.rb

module Api
  class ApiController < ApplicationController
  # logic

/app/controllers/api/home_page_controller.rb /app/controllers/api/home_page_controller.rb

class Api::HomePageController < Api::ApiController
   # logic

/app/controllers/api/users_controller.rb /app/controllers/api/users_controller.rb

class Api::UsersController < Api::ApiController
   # logic

/app/controllers/api/sessions_controller.rb /app/controllers/api/sessions_controller.rb

class Api::SessionsController < Api::ApiController
   # logic

/app/controllers/application_controller.rb /app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.

  protect_from_forgery with: :exception

  # logic

When I try to inherit from RocketPants::Base like the following as mentioned in https://github.com/Sutto/rocket_pants 当我尝试从RocketPants :: Base继承时,如https://github.com/Sutto/rocket_pants中所述

/app/controllers/api/api_controller.rb /app/controllers/api/api_controller.rb

module Api
  class ApiController < RocketPants::Base
    skip_before_filter :verify_authenticity_token

    protect_from_forgery with: :null_session
     # logic

I get errors like this 我收到这样的错误

Started POST "/api/sessions" for 104.155.204.133 at 2015-04-21 22:59:56 +0000
  ActiveRecord::SchemaMigration Load (0.2ms)  SELECT "schema_migrations".* FROM "schema_migrations"

ActionController::RoutingError (undefined method `protect_from_forgery' for Api::ApiController:Class):
  app/controllers/api/api_controller.rb:11:in `<class:ApiController>'
  app/controllers/api/api_controller.rb:2:in `<module:Api>'
  app/controllers/api/api_controller.rb:1:in `<top (required)>'
  app/controllers/api/sessions_controller.rb:2:in `<top (required)>'


  Rendered /usr/local/rvm/gems/ruby-2.1.4/gems/web-console-2.0.0.beta4/lib/action_dispatch/templates/rescues/_trace.html.erb (3.6ms)
  Rendered /usr/local/rvm/gems/ruby-2.1.4/gems/web-console-2.0.0.beta4/lib/action_dispatch/templates/routes/_route.html.erb (40.7ms)
  Rendered /usr/local/rvm/gems/ruby-2.1.4/gems/web-console-2.0.0.beta4/lib/action_dispatch/templates/routes/_table.html.erb (19.0ms)
  Rendered /usr/local/rvm/gems/ruby-2.1.4/gems/web-console-2.0.0.beta4/lib/action_dispatch/templates/rescues/routing_error.html.erb within rescues/layout (266.2ms)
104.155.204.133 - - [21/Apr/2015:22:59:57 +0000] "POST /api/sessions HTTP/1.1" 404 97164 1.1316

I have added the gem and executed bundle install. 我添加了gem并执行了捆绑安装。

Gemfile 的Gemfile

gem 'rocket_pants', '~> 1.10.0'

Can someone kindly guide me how to add rocketpants? 有人可以指导我如何添加火箭裤吗?

Removed the skip_before_filter and protect_from_forgery lines from controller. 从控制器中删除了skip_before_filter和protect_from_forgery行。 All errors are gone. 所有错误都消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Rails4中使用Carrierwave在Blob中保存base64图像? - How to save base64 image in Blob with Carrierwave in Rails4? 如何从Rails4删除JSON格式 - How to remove json format from Rails4 使用Amazon SES通过Rails4发送邮件时如何防止邮件被标记为垃圾邮件 - How to prevent mails from being marked spam while sending through Rails4 using Amazon SES Rails4:如何将Rails应用程序连接到API数据库? - Rails4: How to connect a rails app to an API database? 编写包含Rails4中的嵌套查询的内容 - Writing includes for nested query in rails4 如何在Ruby on Rails中继承自同一基类的多个子类的属性排序? - How can I order by attributes of multiple subclasses that inherit from the same base class in Ruby on Rails? 如何在不继承ActiveRecord :: Base的类中使用Paperclip gem for Rails? - How can I use Paperclip gem for Rails in a class that does not inherit from ActiveRecord::Base? Rails4-如何从基于值的选择中删除选项? - Rails4 - How to drop an option from select based on the value? Rails和ActiveRecord:将方法附加到从ActiveRecord :: Base继承的模型 - Rails & ActiveRecord: Appending methods to models that inherit from ActiveRecord::Base 如何在rails4中使用sass? - How to use sass in rails4?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM