DB中的属性不更改

时间:2015-08-06 21:48:10

标签: mysql ruby-on-rails

我正在尝试更改合同的属性,允许用户“批准”或“拒绝”

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

我没有收到任何错误,但更改未显示在我的数据库中。在我的rails控制台中,似乎操作正在通过。我还在合同控制器中添加了:我的强力参数状态。

这是我的控制器代码段

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

我的控制器params

  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控制台。你会看到合同正在“批准”。 。 。它只是没有保存到数据库。

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

任何帮助都很棒。感谢

2 个答案:

答案 0 :(得分:0)

首先,看起来你拼错了参数名称。将其更改为:

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

...因为您在控制器中使用params[:decision],而不是params[:evaluate]

其次,当您渲染此视图时,我不太确定您对Rails的初始请求中是否存在decision(或evaluate)参数。只是为了确定一切,请使用常规if运算符:

<% decision = {'approve' => 'reject', 'reject' => 'approve'}[@contract.status] %>
<%= link_to("Approve", url_for(decision: decision)) %>

最后,您的链接网址错误。这是正确的:

<%= link_to("Approve", url_for(action: 'evaluate', decision: decision), method: :post) %>

答案 1 :(得分:0)

您遇到的问题与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

尝试一下,看看它是怎么回事。