加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 自定义控制器操作update_attribute返回nil

发布时间:2020-12-17 02:24:27 所属栏目:百科 来源:网络整理
导读:我正在尝试为我的最终学校项目创建我的第一个自定义控制器操作. 在我尝试更新它之后,approved_at属性的值返回nil. 视图显示没有错误,闪回返回成功.有人能指出我正确的方向吗? 如果我没有提供足够的信息,请在评论中告诉我,我将添加所需的文件. registry_requ
我正在尝试为我的最终学校项目创建我的第一个自定义控制器操作.

在我尝试更新它之后,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)’.

编辑:服务器日志

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读