Rails 4 API具有强参数?

 权志龙qzl818_734 发布于 2023-02-09 13:12

我正在使用Rails 4构建一个简单的API,但是使用我的"创建"方法,这一切都非常糟糕.

这是我的路线文件的相关部分:

namespace :api, defaults: { format: 'json' } do
         # /api/... Api::
        scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do
        resources :users
    end
end

这是api/v1/users_controller.rb:

class Api::V1::UsersController < ApplicationController

        protect_from_forgery except: :create
        respond_to :json

        def index
            respond_to do |format|
                format.html {render text: "Your data was sucessfully loaded. Thanks"}
                format.json { render text: User.last.to_json }
            end
        end

        def show
            respond_with User.find(params[:id])
        end

        def create
            respond_with User.create(user_params)
        end

        def update
            respond_with User.update(params[:id], params[:users])
        end

        def destroy
            respond_with User.destroy(params[:id])
        end

        private

            def user_params
              params.require(:user).permit(:name, :age, :location, :genre_ids         => [], :instrument_ids => [])
            end
    end

每当我尝试使用JSON添加API时,我都会收到"{"错误":{"name":["不能为空"]}}"

它可以用我的常规控制器创建一个用户,但我感觉我的API控制器由于强参数而搞砸了.

有关如何在Rails 4中正确执行此操作的任何建议吗?此外,我通过我的用户模型有一些Has-Many-Through关系.API的用户控制器应该可以看到蝙蝠,对吗?

谢谢

编辑:

我现在收到这个错误: 在此输入图像描述

编辑:

{
  "name": "Sally",
  "age": "23",
  "location": "Blue York",
  "genre_ids": [1, 2, 3]
}

再次编辑

即使在我的JSON调用中添加了User参数,它仍然会给我带来相同的错误:user param missing.我是否错误地使用了强参数?在我的"常规"users_controller中,我可以使用我已设置的表单轻松创建用户,但是使用此API控制器,我似乎无法使用JSON创建一个.还有其他建议吗?

再次编辑 这里是从开始到错误的日志

rails s
=> Booting WEBrick
=> Rails 4.0.1 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2013-12-19 14:03:01] INFO  WEBrick 1.3.1
[2013-12-19 14:03:01] INFO  ruby 1.9.3 (2013-02-22) [x86_64-darwin10.8.0]
[2013-12-19 14:03:01] INFO  WEBrick::HTTPServer#start: pid=53778 port=3000


 Started GET "/api/users" for 127.0.0.1 at 2013-12-19 14:03:02 -0500
 ActiveRecord::SchemaMigration Load (0.1ms)  SELECT "schema_migrations".* FROM  "schema_migrations"
 Processing by Api::V1::UsersController#index as JSON
 User Load (0.2ms)  SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
 Rendered text template (0.0ms)
 Completed 200 OK in 142ms (Views: 27.8ms | ActiveRecord: 0.6ms)
 [2013-12-19 14:03:03] WARN  Could not determine content-length of response body. Set   content-length of the response or set Response#chunked = true
 [2013-12-19 14:03:03] WARN  Could not determine content-length of response body. Set  content-length of the response or set Response#chunked = true


 Started POST "/api/users" for 127.0.0.1 at 2013-12-19 14:03:37 -0500
 Processing by Api::V1::UsersController#create as JSON
 Completed 400 Bad Request in 1ms

 ActionController::ParameterMissing (param not found: user):
 app/controllers/api/v1/users_controller.rb:40:in `user_params'
 app/controllers/api/v1/users_controller.rb:20:in `create'


 Rendered /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-  4.0.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.7ms)
 Rendered /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-4.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
 Rendered /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-4.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.8ms)
 Rendered /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-  4.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout   (31.6ms)

编辑#6 这是我的"真正的"users_controller,它存在于我的应用程序而不是我的API中.表单从此控制器创建用户,而不是API控制器.

class UsersController < ApplicationController

  def index
    @users = User.all
    @genres = Genre.all
    @instruments = Instrument.all

    render json: @users
  end

  def new
    @user = User.new
  end

  def show
    @user = User.find(params[:id])
  end

  def create
    @user = User.new(user_params)

    if @user.save
      render json: @user, status: :created, location: @user
    else
      render json: @user.errors, status: :unprocessable_entity
    end
  end

  private

    def user_params
      params.require(:user).permit(:name, :age, :location, :genre_ids => [], :instrument_ids => [])
    end
end

还有 - 用户表格

<%= form_for(@user) do |f| %> <%= f.label :name %> <%= f.text_field :name %> <%= f.label :age %> <%= f.text_field :age %> <%= f.label :email %> <%= f.text_field :email %> <%= f.label :location %> <%= f.text_field :location %>
<% Genre.all.each do |genre| %> <%= check_box_tag "user[genre_ids][]", genre.id %> <%= genre.name %>
<% end %>
<% Instrument.all.each do |instrument| %> <%= check_box_tag "user[instrument_ids][]", instrument.id %> <%= instrument.name %>
<% end %> <%= f.submit "Create My Account!" %> <% end %>
<%= users_path %>

这是我的user.rb文件

class User < ActiveRecord::Base

validates :name, presence: true, length: { maximum: 50 }
has_many :generalizations
has_many :genres, through: :generalizations

has_many :instrumentations
has_many :instruments, through: :instrumentations

end

这是我的路线文件中的内容:

namespace :api do
   namespace :v1 do
      resources :users
   end
end

我的POST请求

POST/api/v1/users HTTP/1.1主机:localhost:3000 Cache-Control:no-cache

{"user":{"name":"Sally","age":"23","location":"Blue York","genre_ids":[1,2,3]}}

UPDATE

我改变了我的强力参数:

def user_params
    params.require(:user).permit(:name, :age, :location, :genre_ids => [],   :instrument_ids => []) if params[:user]
end

因此,最后的"if"语句会使错误消失,但每当我发布到我的API时,它都会返回"null".所以这可能与以前一样,但以不同的方式显示.但是,与此同时,它可能是进步!

这是以前更新的日志

Started POST "/api/v1/users" for 127.0.0.1 at 2013-12-21 11:38:03 -0500
Processing by API::V1::UsersController#create as */*
(0.1ms)  begin transaction
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
(0.1ms)  rollback transaction
User Load (0.1ms)  SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
Rendered text template (0.0ms)
Completed 200 OK in 20ms (Views: 0.3ms | ActiveRecord: 0.6ms)

最后更新 我遗漏了一些东西,但主要做的是我缺少"Content-Type - application/json"作为我的Header.我觉得很有成就感!感谢大家的帮助!

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有