Rails更新方法无法在Rails 5中更新记录

时间:2019-01-19 13:17:30

标签: ruby-on-rails ruby-on-rails-5

我有一个Rails应用程序,在我的Rails应用程序中,我有一个管理部分。我执行以下操作以更新记录

<%= link_to "Approve", admins_transaction_path(transaction, transaction: {status: 'approved'}), method: :put %>

在我的控制器中,看起来像这样

class Admins::TransactionsController < AdminsController
  before_action :get_transaction_id, only: [:update]

  def index
    @transactions = Transaction.where(status: 'pending')
  end

  def update
    @transaction.update_attributes(transactions_params)
    redirect_to admins_transactions_path
  end

  private

  def get_transaction_id
    @transaction = Transaction.find_by(id: params[:id])
  end

  def transactions_params
    params.require(:transaction).permit(:amount, :asset_id, :user_id, :status, :txnref)
  end
end

这是我的日志

    Started PUT "/admins/transactions/1?transaction%5Bstatus%5D=approved" for 127.0.0.1 at 2019-01-19 21:17:51 +0800
    Processing by Admins::TransactionsController#update as HTML
      Parameters: {"authenticity_token"=>"Al0iJGAbwEaLs8jixUOOANQYO7QZzQZWIMp2e2crykLyeGc/JlNHwg1AATqtWMzidhK87Td199b+P0MLguAzVw==", "transaction"=>{"status"=>"approved"}, "id"=>"1"}
      Admin Load (0.8ms)  SELECT  "admins".* FROM "admins" WHERE "admins"."id" = $1 ORDER BY "admins"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
      Transaction Load (0.6ms)  SELECT  "transactions".* FROM "transactions" WHERE "transactions"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
       (0.3ms)  BEGIN
      Asset Load (0.4ms)  SELECT  "assets".* FROM "assets" WHERE "assets"."slug" = $1 LIMIT $2  [["slug", "cash"], ["LIMIT", 1]]
      User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
      Asset Load (0.4ms)  SELECT  "assets".* FROM "assets" WHERE "assets"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
       (0.3ms)  COMMIT
Redirected to http://localhost:3000/admins/transactions
Completed 302 Found in 39ms (ActiveRecord: 6.8ms)

我的交易模型

create_table "transactions", force: :cascade do |t|
    t.float "amount"
    t.string "txnref"
    t.string "status"
    t.integer "asset_id"
    t.integer "user_id"
    t.datetime "deleted_at"
    t.string "type"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["deleted_at"], name: "index_transactions_on_deleted_at"
  end

但是它不会更新我的记录。

令人莫名其妙,我不确定自己在做什么不对。

欢迎提出任何想法。

1 个答案:

答案 0 :(得分:0)

更改

<%= link_to "Approve", admins_transaction_path(transaction, transaction: {status: 'approved'}), method: :put %>

收件人

<%= link_to "Approve", approve_path(transaction), method: :path%>

控制器

def approve
    @transaction.update(:status=>'approved')
    redirect_to admins_transactions_path
  end

路线

patch '#yourcontroller/:id/approve',to:'#yourcontroller#approve' , as: :approve