如何从视图按钮更新行单击<RoR>

时间:2020-02-26 06:07:55

标签: ruby-on-rails

我正在尝试构建Rails应用程序上的ruby,当用户单击按钮时,几行数据会更新。每当我单击按钮时,在PagesController#call_action中都会得到一个 ActiveRecord :: StatementInvalid SQLite3 :: SQLException:没有这样的列:attsances.2 错误。我猜测数据没有正确传递,因此查找无法正常工作,但我不确定为什么无法正常工作。

视图中的内容

<%= link_to 'Approve',
            {controller: 'pages',
             action: 'call_action',
             store_id: @store.id,
             period_id: period.id},
            class: 'button' %>

我的pages_controller.rb

def call_action

    @attendances = Attendance.where(params[:store_id] => :store_id,params[:period_id] => :period_id)
    .update_all(status: "Approved")
        if @attendances!=nil
         @attendances.each do |var|
            var.save!
         end
         redirect_to root_path
      end
end

我的路线文件

get '/pages/call_action' => 'pages#call_action', :as => :call_action

我的模式

create_table "attendances", force: :cascade do |t|
    t.integer "period_id", null: false
    t.integer "employee_id", null: false
    t.integer "store_id", null: false
    t.decimal "hours_assigned"
    t.decimal "hours_worked"
    t.decimal "days_worked"
    t.decimal "regular_holidays_worked"
    t.decimal "special_holidays_worked"
    t.decimal "leaves"
    t.decimal "rest_days"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.integer "oic"
    t.string "status"
    t.index ["employee_id"], name: "index_attendances_on_employee_id"
    t.index ["period_id"], name: "index_attendances_on_period_id"
    t.index ["store_id"], name: "index_attendances_on_store_id"
  end

我在终端中得到的东西

Processing by PagesController#call_action as HTML
  Parameters: {"period_id"=>"9", "store_id"=>"2"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 3], ["LIMIT", 1]]
  Attendance Update All (0.8ms)  UPDATE "attendances" SET "status" = ? WHERE "attendances"."2" = ? AND "attendances"."9" = ?  [["status", "Approved"], ["2", "store_id"], ["9", "period_id"]]
  ↳ app/controllers/pages_controller.rb:13:in `call_action'
Completed 500 Internal Server Error in 6ms (ActiveRecord: 1.0ms | Allocations: 2936)

1 个答案:

答案 0 :(得分:1)

全部更新 返回更新的记录数。 因此, @出席人数将没有记录。 你应该做类似的事情

@attendances = Attendance.where(store_id: params[:store_id],
                               period_id: params[:period_id]) 
@attendances.update_all(status: "Approved")

而且不需要迭代

相关问题