gpt4 book ai didi

mysql - 数据库中的属性未更改

转载 作者:行者123 更新时间:2023-11-29 22:09:05 25 4
gpt4 key购买 nike

我正在尝试更改契约(Contract)的属性,允许用户“批准”或“拒绝”

  <%= link_to_unless_current("Approve", { decision: "approve" }) %>
<%= link_to_unless_current("Reject", { decision: "reject" }) %>

我没有收到任何错误,但更改没有显示在我的数据库中。在我的 Rails 控制台中, Action 似乎正在传递。我还在合约 Controller 中将 :status 添加到了强参数中。

这是我的 Controller 片段

 def evaluate
if params[:decision] == "approve"
@contract.status = "approve"
else params[:decision] == "reject"
@contract.status = "reject"
end
@contract.save
end

还有我的 Controller 参数

  def contract_params
params.require(:contract).permit(:price, :start_date, :end_date, :open_ended, :lessor_contact_name, :lessor_business_name, :lessee_name, :lessee_email, :lessee_address, :lessee_business_name, :lessee_guarantor, :status)
end

这是我的 Rails 控制台。您将看到契约(Contract)正在“批准”。 。 。它只是没有保存到数据库。

Started GET "/spaces/1/contracts/17?decision=approve" for ::1 at 2015-08-07 09:23:27 -0400
ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by ContractsController#show as HTML
Parameters: {"decision"=>"approve", "space_id"=>"1", "id"=>"17"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 2]]
Space Load (0.1ms) SELECT "spaces".* FROM "spaces" WHERE "spaces"."id" = ? LIMIT 1 [["id", 1]]
Property Load (0.1ms) SELECT "properties".* FROM "properties" WHERE "properties"."id" = ? LIMIT 1 [["id", 1]]
Contract Load (0.2ms) SELECT "contracts".* FROM "contracts" WHERE "contracts"."id" = ? LIMIT 1 [["id", 17]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Rendered contracts/_landlord.html.erb (6.5ms)
Rendered contracts/show.html.erb within layouts/application (11.6ms)
Rendered layouts/_header.html.erb (0.8ms)
Completed 200 OK in 563ms (Views: 494.5ms | ActiveRecord: 2.1ms)

这是我的路线

  resources :spaces do
collection do
get 'search'
end
resources :galleries do
resources :pictures
end
resources :contracts do
post 'evaluate', on: :member
end
resources :reviews
end

任何帮助都很棒。谢谢

最佳答案

您遇到的问题与rails在数据库中更新和保存数据的方式直接相关。在您的 evaluate 方法中,您将 status 的值设置为批准或拒绝,但仅设置该值实际上并不保存该值。因此,您可以执行以下两件事之一。

1) 在您的 evaluate 方法中,您可以在设置后保存记录:

def evaluate
if params[:decision] == "approve"
@contract.status = "approve"
else params[:decision] == "reject"
@contract.status = "reject"
end
@contract.save
end

2) 或者您可以使用 update_attributes 它会自动为您保存:

def evaluate
if params[:decision] == "approve"
@contract.update_attributes(status: "approve")
else params[:decision] == "reject"
@contract.update_attributes(status: "reject")
end
end

尝试一下,看看效果如何。

关于mysql - 数据库中的属性未更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31866382/

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