我使用的是API而不是数据库,所以我没有使用ActiveRecord,而是使用ActiveModel(我大多喜欢这里:railscasts.com/episodes/219-active-model)
事情是,当我尝试编辑项目时(在我的情况下是停车场),表单的操作仍然是创建的操作而不是更新。
所以当我继续/停车/ 2 /编辑编辑停车时,表格仍然是:
<form accept-charset="UTF-8" action="/parkings" class="form-horizontal" id="new_parking" method="post">
当它应该更像是把隐藏字段和停车场/ 2作为动作:
<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="✓" /><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&amp; new.html.erb,只是
<%= render 'form' %>
控制器
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 =“”添加我的停车ID,并添加:method =&gt; “put”在我的form_for中,我的记录成功更新。
这里的真正问题实际上是form_for action&amp;我在编辑停车场时没有得到更新的方法,如果我要添加一个新的停车场,那就好了。
我还在学习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
答案 0 :(得分:6)
我认为您的问题出在您的模型的persisted?
方法中。因为它总是返回false,所以Rails总是认为它正在为新创建的记录构建一个表单,所以它使用POST并提交到集合URL。
您需要在该方法中使用某种逻辑,以便现有记录返回true并且新记录返回false。
答案 1 :(得分:1)
您好朋友可以告诉表单生成器使用哪种方法。试试
<%= form_for(@parking, :method => ["new", "create"].include?(action_name) ? :post : :put,
:html => { :class => "form-horizontal" }) do |f| %>
...
<% end %>
答案 2 :(得分:-1)
如果您不使用ActiveRecord,则应使用'form_tag'代替'form_for'