ruby-on-rails – 没有ActiveRecord的form_for,表单操作不更新

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 没有ActiveRecord的form_for,表单操作不更新前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用的是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="&#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' %>

调节器

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,: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 => “put”在我的form_for中,我的记录成功更新.

这里真正的问题实际上是form_for action&我正在编辑停车时没有得到更新的方法,如果我要添加一个新的停车库仍然存在.

我还在学习Rails,很抱歉,如果有些信息不清楚!

谢谢!

解决方案—

坚持?不应该只返回false,我的模型需要定义一个返回对象id的方法(所以他们可以更新action =“”)所以这是我更新的模型:

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

  attr_accessor :objectId,: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

解决方法

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

在该方法中需要某种逻辑,以便现有记录返回true并且新记录返回false.

原文链接:https://www.f2er.com/ruby/267828.html

猜你在找的Ruby相关文章