1

好的,所以我只是想编写一些简单的代码来更新给定 ID 的记录。这就是它的样子。

def updaterecord
    bathroom = Bathroom.find(params[:key])
    bathroom.name= params[:name],
    #bathroom.bathroomtype = params[:bathroomtype],
    bathroom.street = params[:street]
    #bathroom.city = params[:city],
    #bathroom.state = params[:state],
    #bathroom.country = params[:country],
    #bathroom.postal = params[:postal],
    #bathroom.access = params[:access],
    #bathroom.directions = params[:directions],
    #bathroom.comment = params[:commment],
    #bathroom.avail = params[:avail]
    bathroom.save
  end 

问题是,虽然我试图更新每个单独的属性,但它们都被连接到名称字段。例如,上面的这段代码将 name 字段设置为 name 加上地址。我不知道为什么?

如果我在更新后尝试查询,这就是控制台的样子。

  Bathroom Load (0.2ms)  SELECT "bathrooms".* FROM "bathrooms" WHERE "bathrooms"."ID" = ? LIMIT 1  [["ID", 4017]]
=> #<Bathroom ID: 4017, name: "--- \n- ttyt\n- 113 some\n", bathroomtype: "0", street: "113 some", city: "Portland", state: "OR", country: "United States", postal: "97217", lat: #<BigDecimal:1109f2890,'0.4558056E2',12(12)>, lon: #<BigDecimal:1109f27c8,'-0.122677857E3',12(16)>, access: "0", directions: "", comment: nil, created: "2012-06-08 17:19:03.420329", modifed: "", avail: "1", slug: "", source: "Squat">

这就是帖子值的样子:

post values = key=4017&name=www&bathroomtype=0&street=7540 N Interstate Ave&city=Portland&state=OR&country=United States&postal=97217&access=0&directions=&comment=<null>&avail=1

为什么我不能让每个字段单独更新?对不起,我对发生了什么感到困惑?

4

2 回答 2

2

我认为你可能在行尾有一个不必要的逗号。

它应该是:

def updaterecord
    bathroom = Bathroom.find(params[:key])
    bathroom.name= params[:name]
    bathroom.street = params[:street]
    bathroom.save
end
于 2012-10-30T02:48:15.973 回答
0

看起来你没有正确地对你的帖子值进行 urlencoded

一个简单的

puts params.inspect

或者

pp params

应该注销 params 对象。你也可以使用

render :text => params.inspect

将其打印到您的结果 html

于 2012-10-30T02:50:58.650 回答