我使用的是API而不是数据库,所以我使用的不是ActiveRecord,而是ActiveModel(我在这里大多是这样做的:railscasts.com/episodes/219-active-model)

问题是,当我尝试编辑项目(在我的情况下是 parking )时,表单的操作仍然保留为创建操作,而不是更新操作。

因此,当我继续/parkings/2/edit编辑 parking 场时,该表格仍为:

<form accept-charset="UTF-8" action="/parkings" class="form-horizontal" id="new_parking" method="post">

什么时候应该更像是放置隐藏字段并以parkings/2作为 Action :
<form accept-charset="UTF-8" action="/parkings/2" class="form-horizontal" id="edit_parking" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="_method" type="hidden" value="put" />

有人知道form_for的方法和操作是根据路线设置的吗?我想做的事情就像在数据库中使用ActiveRecord一样紧密。

这是一些代码:

_form.html.erb
<%= form_for(@parking, :html => { :class => "form-horizontal" }) do |f| %>
...
<% end %>

edit.html.erb和new.html.erb,只是
<%= render 'form' %>

Controller
class ParkingsController < ApplicationController
  def index
    @parkings = Parse.get("Parking")

    respond_to do |format|
      format.html
      format.json { render :json => @parking }
    end
  end

  def new
    @parking = Parking.new

    respond_to do |format|
      format.html
      format.json { render :json => @parking }
    end
  end

  def edit
    @parking = Parking.find(params[:id])

    respond_to do |format|
      format.html
      format.json { render :json => @parking }
    end
  end

  def create
    @parking = Parking.new(params[:parking])
    if (@parking.save)
       flash[:success] = "Parking was just added!"
       redirect_to :action => "new"
    else
      render :action => "new"
    end
  end

  def update
   # Testing
   parking = Parse.get("Parking", params[:id])
   parking.delete("updatedAt")
   parking["name"] = params[:parking][:name]
   parking.save

   redirect_to :action => "index"
  end

模型
class Parking
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend  ActiveModel::Naming

  attr_accessor :name, :address, :city, :longitude, :latitude, :contributor_name, :contributor_email
  validates_presence_of :name, :address, :city, :longitude, :latitude

  @id = nil

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  def self.find(id)
    @id = id
    raw                       = Parse.get("Parking", @id.to_s)

    parking = Parking.new
    parking.name              = raw["name"]
    parking.address           = raw["address"]
    parking.city              = raw["city"]
    parking.longitude         = raw["location"]["longitude"]
    parking.latitude          = raw["location"]["latitude"]
    parking.contributor_name  = raw["contributorName"]
    parking.contributor_email = raw["contributorEmail"]

    return parking
  end

  def save
    if (!valid?)
      return false
    else
      parking = Parse::Object.new("Parking")

      data =
      {
        :longitude => longitude.to_f,
        :latitude  => latitude.to_f
      }

      point = Parse::GeoPoint.new(data)

      parking["location"]         = point
      parking["name"]             = name
      parking["address"]          = address
      parking["city"]             = city
      parking["contributorName"]  = contributor_name
      parking["contributorEmail"] = contributor_email

      if (parking.save)
        return true
      end
    end
  end

  def persisted?
    false
  end
end

请注意,创建工作正常,如果我使用Web Inspector或Firebug在action =“”表单中添加 parking ID,并在form_for中添加:method =>“put”,则记录会成功更新。

真正的问题实际上是form_for操作和方法,我在编辑 parking 场时不会得到更新,并且就像添加新 parking 场一样。

我仍在学习Rails,如果某些信息不清楚,抱歉!

谢谢!

--- 解决方案-

坚持吗?不仅应该返回false,而且我的模型需要定义一个返回对象ID的方法(以便他们可以更新action =“”),所以这是我更新的模型:
class Parking
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend  ActiveModel::Naming

  attr_accessor :objectId, :name, :address, :city, :longitude, :latitude, :contributor_name, :contributor_email
  validates_presence_of :name, :address, :city, :longitude, :latitude

  @id = nil

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  def self.find(id)
    raw                       = Parse.get("Parking", id.to_s)

    parking = Parking.new
    parking.objectId          = id
    parking.name              = raw["name"]
    parking.address           = raw["address"]
    parking.city              = raw["city"]
    parking.longitude         = raw["location"]["longitude"]
    parking.latitude          = raw["location"]["latitude"]
    parking.contributor_name  = raw["contributorName"]
    parking.contributor_email = raw["contributorEmail"]

    return parking
  end

  def save
    if (!valid?)
      return false
    else
      parking = Parse::Object.new("Parking")

      data =
      {
        :longitude => longitude.to_f,
        :latitude  => latitude.to_f
      }

      point = Parse::GeoPoint.new(data)

      parking["location"]         = point
      parking["name"]             = name
      parking["address"]          = address
      parking["city"]             = city
      parking["contributorName"]  = contributor_name
      parking["contributorEmail"] = contributor_email

      if (parking.save)
        return true
      end
    end
  end

  def update_attributes(aParking)
    parking = Parse.get("Parking", @id.to_s)
    parking.delete("updatedAt")

    parking["name"] = aParking["name"]
    parking.save

    return true
  end

  def destroy
    parking = Parse.get("Parking", @id)
    #parking.parse_delete
  end

  def id
    return self.objectId
  end

  def persisted?
    !(self.id.nil?)
  end
end

最佳答案

我认为您的问题出在模型的persisted?方法中。由于它始终返回false,因此Rails始终认为它正在为新创建的记录构建表单,因此它使用POST并提交到集合URL。

您需要在该方法中使用某种逻辑,以便现有记录返回true,而新记录返回false。

10-08 04:29