- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我将使用 Ruby on Rails 创建 restful API。我想创建、删除、显示和更新数据。所有这些都必须是 JSON 才能在 Android 设备中获取。我还使用 Postman 检查我的 API。这就是我所做的:
我的 Controller :
class Api::V1::UsersController < ApplicationController
respond_to :json
def show
respond_with User.find(params[:id])
end
def create
user=User.new(user_params)
if user.save
render json: user, status: 201
else
render json: {errors: user.errors}, status: 422
end
end
def update
user=User.find(params[:id])
if user.update(user_params)
render json: user, status:200
else
render json: {erros: user.errors},status: 422
end
end
def destroy
user=User.find(params[:id])
user.destroy
head 204
end
private
def user_params
params.require(:user).permit(:email,:password,:password_confirmation)
end
end
这是我的路线文件:
Rails.application.routes.draw do
devise_for :users
namespace :api, defaults:{ format: :json } do
namespace :v1 do
resources :users, :only=>[:show,:create,:update,:destroy]
end
end
end
并且还在我的 Gemfile 中添加了以下代码:
gem "devise"
gem 'active_model_serializers'
我不知道为什么当我想通过 postman 创建时出现以下错误:
ActionController InvalidAuthenticityToken in Api::V1::UsersController#create
最佳答案
您需要在 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
end
到
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: :null_session
end
编辑
更好的方法是跳过特定 Controller 的身份验证
class Api::V1::UsersController < ApplicationController
skip_before_action :verify_authenticity_token
respond_to :json
# ...
end
关于android - ActionController InvalidAuthenticityToken in Api::V1::UsersController#create,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41619177/
我在玩 cucumber 时发现我的 Rspec 测试开始失败。在我安装 cucumber 之前一切都运行良好。有任何想法吗? 日志 /Users/szymonborucki/.rbenv/versi
我知道这已经被提出 a few times在SO上,但我无法取得进展。我在 Rails 5.0.7.2 上。 我收到 ActionController::InvalidCrossOriginReque
以下是由我的 Rails 应用程序中的表单引起的错误: Processing UsersController#update (for **ip** at 2010-07-29 10:52:27) [P
我有这个遗留的 Spree 应用程序,带有 2 个带有继承回调的装饰 Controller : Spree::Api::BaseController.class_eval do before_ac
我很好奇这一般意味着什么。 但这里是细节.. 我正在做一个可排序的 jquery 项目,涉及这个 rails Action : def update_order params[:media].ea
我收到来自 "button" %> 线路的无路由匹配错误. 在我添加 gem 之前,这曾经有效,但现在它停止工作了。我尝试过收藏,但我没有运气,因为那是以前的地方。 路线: resources :
我发现使用诸如 foo.bar.uk.com 之类的域会导致 Rails扔一个 ActionController::InvalidAuthenticityToken提交任何表格时。 foo.bar.c
我有一个使用 ActionController::Metal on Rails 4.1.6 的 Api Controller ,如下所示: class Api < ActionController::
在我的 ActionController::Live 监听大约 5 分钟后,我在浏览器中看到以下内容,并且我的 EventSource 断开了连接。 EventSource 的响应具有不是“text/
我正在使用 Rail4 并尝试通过 GET 请求发送一些纯文本: def get_text respond_to do |format| format.text { render :tex
我的道路有问题,我不明白他为什么告诉我“Dashboard::TasksController”因为在我的 Controller 文件中包含文件 dashbaord/tasks.rb class Tas
我的 Controller 页面有问题。顺便说一句,我想执行 localhost:3000/article/donat?author_id=4,这意味着我只想查看 author_id = 4 的文章我
我正在运行一个 rails 应用程序,我有一个简单的显示操作,其中的代码类似于以下内容: @post = Post.find(params[:id]) 所以如果你去帖子/1 例如,如果有帖子,您将看到
在 Rails 4.1.6 中,我收到 InvalidAuthenticityToken运行使用脚手架生成的测试时出错。有没有办法禁用特定测试的真实性 token 检查? rails g scaffo
我正在努力使 ActionController::UnknownFormat不会在生产中引发异常报告。我正在使用 Rails 4 并认为这样的事情可以解决问题,但似乎没有什么区别: applicati
我正在尝试使用 Rails 4 的实时流实现文本/事件流。它工作得很好,我遇到的唯一麻烦是我无法在不发送任何消息的情况下检查连接是否有效。 我想出的唯一解决方案是使用循环滴答发生器制作支持 chann
我已经阅读了所有可以找到的类似问题,但仍然无法弄清我的问题。 # routes.rb Rails.application.routes.draw do resources :lists, only
我在 application_contoller.rb 中有一个方法,其中包含对 send_file 的调用。我从 javascript 渲染的部分调用它,并收到以下错误 - ActionView::
所以...第一次向 StackOverflow 提问... 我按照 Rails Guides 和 RailsApps 项目将现有的 Rails 4.2.5 应用程序(使用 Ruby 2.2.4)转换为
我有一个关于 Rails ActionController::Live 的问题 最后我想向用户展示 FFMPEG 的进展,但现在我想让这个最小的示例运行: Rails media_controller
我是一名优秀的程序员,十分优秀!