gpt4 book ai didi

ruby-on-rails - 自定义 Controller 操作 update_attribute 返回 nil

转载 作者:数据小太阳 更新时间:2023-10-29 07:33:47 25 4
gpt4 key购买 nike

我正在尝试为我的期末学校项目创建我的第一个自定义 Controller 操作。

approved_at 属性的值在我尝试更新后返回 nil。

View 显示没有错误,flash返回成功。谁能指出我正确的方向?

如果我没有提供足够的信息,请在评论中告诉我,我会添加所需的文件。

registry_requests_controller.rb

class RegistryRequestsController < ApplicationController
def index
@registry_requests = RegistryRequest.all
end

def show
@registry_request = RegistryRequest.find(params[:id])
end

def approve
@approval = RegistryRequest.find(params[:id])

if @approval.save
@approval.update_attribute(:approved_at, true)

flash[:notice] = "The vehicle was approved for the Registry."
redirect_to request.referer
else
flash[:error] = "There was an error approving the vehicle. Please try again."
end
end

def deny
@registry_request.where(denied_at: true)
end
end

rails 控制台

irb(main):003:0> RegistryRequest.first
RegistryRequest Load (0.2ms) SELECT "registry_requests".* FROM "registry_requests" ORDER BY "registry_requests"."id" ASC LIMIT 1
=> #<RegistryRequest id: 1, approved_at: nil, denied_at: nil, notes: nil, vehicle_id: 31, created_at: "2015-07-23 14:01:26", updated_at: "2015-07-23 14:01:26">

提高参数

Parameters:

{"_method"=>"patch",
"authenticity_token"=>"F9RwL4WZbo8fXnjLqnQaG5gpVxW60JGSuqq5hPEeCJKGwPolJTNRIOmzyrX/HAsEmlwLQ9CXfS9PmNcK1OL9+A==",
"id"=>"1"}

registry_requests#show

<div><%= @registry_request.vehicle.make %></div>
<div><%= @registry_request.vehicle.model %></div>
<div><%= @registry_request.vehicle.description %></div>
<%= link_to "Approve", approve_registry_request_path, method: :patch %>
<%= link_to "Deny", deny_registry_request_path(request), method: :patch %>

注册表请求迁移

class CreateRegistryRequests < ActiveRecord::Migration
def change
create_table :registry_requests do |t|
t.datetime :approved_at
t.datetime :denied_at
t.text :notes
t.references :vehicle, index: true, foreign_key: true

t.timestamps null: false
end
end
end

我尝试过的

我尝试将 .save 添加到 if @approval,并在 update_attribute 行下方添加了 @approval.save。如果我使用 update_attributes,则会抛出错误,提示“参数数量错误(2 为 1)”。

编辑:服务器日志

Started PATCH "/registry_requests/1/approve" for ::1 at 2015-07-27 10:47:15 -0400
Processing by RegistryRequestsController#approve as HTML
Parameters: {"authenticity_token"=>"trCMgfHPT0LL5fOxVDmQyzRmpyFF4B/WeUHtzkvyHR0npAaLUWVw7T0IQc8BUYHUNhP7dy+n82uMc4NAbg7odw==", "id"=>"1"}
RegistryRequest Load (0.1ms) SELECT "registry_requests".* FROM "registry_requests" WHERE "registry_requests"."id" = ? LIMIT 1 [["id", 1]]
(0.0ms) begin transaction
(0.0ms) commit transaction
Redirected to http://localhost:3000/registry_requests/1
Completed 302 Found in 2ms (ActiveRecord: 0.2ms)


Started GET "/registry_requests/1" for ::1 at 2015-07-27 10:47:15 -0400
Processing by RegistryRequestsController#show as HTML
Parameters: {"id"=>"1"}
RegistryRequest Load (0.1ms) SELECT "registry_requests".* FROM "registry_requests" WHERE "registry_requests"."id" = ? LIMIT 1 [["id", 1]]
Vehicle Load (0.1ms) SELECT "vehicles".* FROM "vehicles" WHERE "vehicles"."id" = ? ORDER BY created_at DESC LIMIT 1 [["id", 31]]
Rendered registry_requests/show.html.erb within layouts/application (1.3ms)
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 6]]
Completed 200 OK in 72ms (Views: 71.2ms | ActiveRecord: 0.3ms)

最佳答案

因为approved_at是一个datetime字段,更新值的时候,传递了boolean
尝试使用当前时间戳更新 approved_at 属性,如下所示。

def approve
@approval = RegistryRequest.find(params[:id])
if @approval.update_attribute(:approved_at, Time.now)
flash[:notice] = "The vehicle was approved for the Registry."
redirect_to request.referer
else
flash[:error] = "There was an error approving the vehicle. Please try again."
end
end

关于ruby-on-rails - 自定义 Controller 操作 update_attribute 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31655976/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com