form_for没有ActiveRecord,表单操作不更新

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

事情是,当我尝试编辑一个项目(在我的情况下是一个停车场)时,表单的动作仍然是创建的动作而不是更新。

所以当我继续/停车/ 2 /编辑编辑停车时,表格仍然是:

当它应该更像是把隐藏字段和停车场/ 2作为动作:

 

谁知道根据路线设置form_for的方法和动作? 我正在尝试做的就是将ActiveRecord与数据库一起使用。

这是一些代码:

_form.html.erb

  { :class => "form-horizontal" }) do |f| %> ...  

edit.html.erb和new.html.erb,简单地说

  

调节器

 class ParkingsController  @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 =“”中添加我的停车ID,并在我的form_for中添加:method =>“put”,我的记录会成功更新。

这里真正的问题实际上是form_for动作和方法,当我正在编辑停车时没有得到更新,并且如果我正在添加一个新的停车。

我还在学习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。

你好朋友,你可以告诉表单生成器使用哪种方法。所以试试

 <%= form_for(@parking, :method => ["new", "create"].include?(action_name) ? :post : :put, :html => { :class => "form-horizontal" }) do |f| %> ... <% end %> 

如果您没有使用ActiveRecord,则应使用’form_tag’代替’form_for’