通过http POST在Ruby on Rails应用程序中创建对象的最合适方法是什么?

时间:2012-09-15 19:39:19

标签: ruby-on-rails ruby http post

我对Web开发一般都很陌生,我一直在研究一个我希望(最终)与Android应用程序交互的Ruby on Rails应用程序。我目前正在寻找从Android应用程序发送的http POST创建对象的最合适和安全的方法。

通过使用REST客户端扩展程序试图将POST发送到localhost上的应用程序并让它从POST信息中创建一个对象,我正在接近它以实现此目的。

这是来自Chrome扩展程序的POST的结果:

Started POST "/problems" for 127.0.0.1 at 2012-09-15 14:16:19 -0400
Processing by ProblemsController#create as */*
  Parameters: {"user"=>"7876483097", "latitude"=>"18.378383", "longitude"=>"-67.026201", "ptype"=>"2", "description"=>"Poste roto"}
  ←\[1m←\[36m (0.0ms)←\[0m  ←\[1mbegin transaction←\[0m
  ←\[1m←\[35m (0.0ms)←\[0m  rollback transaction
  Rendered problems/new.html.erb within layouts/application (31.0ms)
  Rendered layouts/_shim.html.erb (0.0ms)
  ←\[1m←\[36mUser Load (0.0ms)←\[0m  ←\[1mSELECT "users".* FROM "users" WHERE "users"."remember_token" = 'gdmKIurcqDOMoDGWE4IBng' LIMIT 1←\[0m
  Rendered layouts/_header.html.erb (4.0ms)
  Rendered layouts/_footer.html.erb (1.0ms)
Completed 200 OK in 93ms (Views: 87.0ms | ActiveRecord: 0.0ms)

出现“回滚事务”的地方意味着create方法中的@ problem.save失败。任何人都可以帮助确定我所缺少的内容或我应该如何改变我的方法来正确地做到这一点。

这是我的模型架构:

  create_table "problems", :force => true do |t|
    t.string   "user"
    t.float    "latitude"
    t.float    "longitude"
    t.integer  "ptype"
    t.string   "description"
    t.integer  "priority"
    t.integer  "status"
    t.datetime "created_at",          :null => false
    t.datetime "updated_at",          :null => false
    t.string   "avatar_file_name"
    t.string   "avatar_content_type"
    t.integer  "avatar_file_size"
    t.datetime "avatar_updated_at"
    t.boolean  "gmaps"
    t.string   "address"
  end

这是我的创建方法:

  def create
    @problem = Problem.new(params[:problem])
    if @problem.save
      flash[:success] = "Problema guardado"
      redirect_to @problem
    else
      @problem.errors.full_messages
      flash.now[:error] = 'Informacion incorrecta'
      render 'new'
    end
  end

这是POST正文:

user=7876483097&latitude=18.378383&longitude=-67.026201&ptype=2&description=Poste+roto

而且,这是问题模型和验证

class Problem < ActiveRecord::Base

    acts_as_gmappable :latitude => 'latitude', :longitude => 'longitude', :process_geocoding => :geocode?,
                  :address => "address", :normalized_address => "address",
                  :msg => "Lo sentimos, ni Google puede localizar esa direccion"

    attr_accessible :user, :latitude, :longitude, :ptype, :description, :avatar, :address
    validates(:user, presence: true)
    validates(:latitude, presence: true)
    validates(:longitude, presence: true)
    validates(:ptype, presence: true)

提前致谢。

2 个答案:

答案 0 :(得分:2)

问题在于您作为POST正文发送的字段的命名。

请注意,Problem的新实例是使用params[:problem]构建的。

因此,字段应该被命名为problem[user]problem[latitute]等,而不仅仅是userlatitude

答案 1 :(得分:0)

这是帖子结构应该是这样的:

Parameters: {"problem"=>{"user"=>"7876483097", "latitude"=>"18.378383", "longitude"=>"-67.026201", "ptype"=>"2", "description"=>"Poste roto"}}

而不是你拥有的:

Parameters: {"user"=>"7876483097", "latitude"=>"18.378383", "longitude"=>"-67.026201", "ptype"=>"2", "description"=>"Poste roto"}

问题可能只在于您使用其余客户端扩展构建帖子的方式。我发现通过命令行或终端使用curl更容易。

尝试在终端或命令行中使用类似的东西:

curl -H "Content-Type:application/json" -H "Accept:application/json" -d "{\"problem\":{\"user\":\"7876483097\", \latitude\":\"18.378383\", \"longitude\":\"-67.026201\",\"ptype\":\"2\",\"description\":\"posteroto\"}}" http://localhost:3000/problems