【问题标题】:How to save rails object from a JSON POST?如何从 JSON POST 中保存 rails 对象?
【发布时间】:2013-01-28 16:30:46
【问题描述】:

我想知道如何通过 JSON POST 在 rails 3 中保存对象?

问题在于 UsersController.rb 无法识别 JSON POST 参数并假定请求中没有参数。

我有两个相同的创建方法,只是为了调试问题。提前致谢!

Routes.rb

post 'users'          => "users#create"
match 'users/create'  => "users#create_user", :via => [ :post]

用户.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable,
         :validatable, :token_authenticatable #, :omniauthable

  has_many :leader_boards

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :authentication_token , :first_name, :large_profile_image_url, :last_name, :profile_image_url, :provider, :uid

  validates :email, :uniqueness => { :case_sensitive => false }
  validates :email, :first_name, :last_name, :presence => true
  validates :password, :password_confirmation, :presence => true
  validates :password, :length => { :minimum => 8 }

  def name
    return self.first_name.to_s + ' ' + self.last_name.to_s
  end
end

UsersController.rb

module Api
  module V1
    class UsersController < ApplicationController
      before_filter :authenticate_user!, :except => [:create, :show, :update, :create_user, :facebook,:leader_board]
      respond_to :json

      def create
        @user = User.create(params[:user])
        begin
          @user.reset_authentication_token!
        rescue

        end
        respond_with @user
      end

      def create_user
        begin
          @user = User.new
          @user.email = params[:email]
          @user.password  = params[:password]
          @user.password_confirmation   = params[:password_confirmation]
          @user.first_name = params[:first_name]
          @user.last_name  = params[:last_name]
          @user.save
        rescue

        end
        respond_with @user
      end

JSON POST

{ “电子邮件”:“jordanpass@jordanpass.cpm”,“first_name”:“mike”,“last_name”:“jordan”, “密码”:“乔丹密码”,“密码确认”:“乔丹密码”}

回应

{"errors":{"email":["can't be blank","can't be blank"],"password":["can't be blank","can't be blank","is too short (minimum is 8 characters)"],"first_name":["can't be blank"],"last_name":["can't be blank"],"password_confirmation":["can't be blank"]}}

示例帖子替代屏幕截图

【问题讨论】:

标签: ruby-on-rails json


【解决方案1】:

您的 JSON 帖子:

{ “email”:“jordanpass@jordanpass.cpm”,“first_name”:“mike”,“last_name”:“jordan”,“密码”:“jordanpass”,“password_confirmation”:“jordanpass”}

不正确。由于控制器正在寻找 params[:user],因此您的 JSON 应该是:

{“用户”:{“电子邮件”:“jordanpass@jordanpass.cpm”,“first_name”:“mike”,“last_name”:“jordan”,“密码”:“jordanpass”,“password_confirmation”:“乔丹通行证" } }

【讨论】:

  • 谢谢,我试过了,我得到了同样的回应。这是截图:i.stack.imgur.com/u78iu.png
  • @ipegasus 你最后是怎么解决这个问题的?我有同样的问题,我的 json 是 100% 正确的
  • 你今天救了我!谢谢。
猜你喜欢
  • 2016-12-06
  • 2016-01-14
  • 1970-01-01
  • 2017-06-28
  • 1970-01-01
  • 2011-10-02
  • 1970-01-01
  • 2014-10-14
  • 2015-12-28
相关资源
最近更新 更多