我正在尝试为我的最终学校项目创建我的第一个自定义控制器操作。
我尝试更新后,approved_at属性的值返回nil。
视图显示没有错误,闪回返回成功。有人能指出我正确的方向吗?
如果我没有提供足够的信息,请在评论中告诉我,我将添加所需的文件。
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 console
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#展示
<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)&#39;。
编辑:服务器日志
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)
答案 0 :(得分:3)
这是因为string f(string a)
是approved_at
,在更新价值时,会传递datetime field
。
尝试使用当前时间戳更新boolean
属性,如下所示。
approved_at