ruby-on-rails - 使用 .increment 方法时,控制器未更新数据库中的值
问题描述
我有一个 Rails 控制器,当我调用该.increment!
方法时,它没有更新数据库中的值。
有趣的是,相同的代码在create
方法中起作用
这是控制器代码
class RequestsController < ApplicationController
before_action :set_request, only: [:show, :edit, :update, :destroy]
# GET /requests
# GET /requests.json
def index
@requests = Request.all
end
# GET /requests/1
# GET /requests/1.json
def show
end
# GET /requests/new
def new
@request = Request.new
end
# GET /requests/1/edit
def edit
end
# POST /requests
# POST /requests.json
def create
@request = Request.new(request_params)
respond_to do |format|
if @request.save
format.html { redirect_to @request, notice: 'Request was successfully created.' }
format.json { render :show, status: :created, location: @request }
@request.increment!(:voteCount)
else
format.html { render :new }
format.json { render json: @request.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /requests/1
# PATCH/PUT /requests/1.json
def update
respond_to do |format|
if @request.update(request_params)
format.html { redirect_to @request, notice: 'Request was successfully updated.' }
format.json { render :show, status: :ok, location: @request }
else
format.html { render :edit }
format.json { render json: @request.errors, status: :unprocessable_entity }
end
end
end
# DELETE /requests/1
# DELETE /requests/1.json
def destroy
@request.destroy
respond_to do |format|
format.html { redirect_to requests_url, notice: 'Request was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_request
@request = Request.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def request_params
params.require(:request).permit(:artist, :title, :voteCount)
end
def upvote
puts 'upvote'
request = Request.find(params[:request_id])
render json: { voteCount: request.voteCount }
@request.increment!(:voteCount)
request.increment!(:voteCount)
request.save
request.reload
end
end
这是html
<tbody>
<% @requests.each do |request| %>
<tr data-request-id="<%= request.id %>">
<td id="artist" style="text-align: center"><%= request.artist %></td>
<td id="songTitle" style="text-align: center"><%= request.title %></td>
<td style="text-align: center" id="voteCount<%= request.id %>"><%= request.voteCount %></td>
<td style="text-align: center"><%= button_to 'Vote', request_upvote_path(request.id), remote: true, method: :post, onclick: "upvote(#{request.id})", class: 'upvote', id:"voteButton#{request.id}" %></td> </tr>
<% end %>
这是更新页面计数的 JS
$("#ClickMe").attr("disabled", "disabled");
function upvote(id) {
var count = document.getElementById("voteCount" + id).innerHTML;
count = parseInt(count);
count = count + 1;
count = count.toString();
document.getElementById("voteCount" + id).innerHTML = count;
document.getElementById("voteButton" + id).disabled = true;
}
添加路由.rb
Rails.application.routes.draw do
resources :requests do
post 'upvote', to: 'requests#upvote'
end
end
解决方案
在回答你的问题之前我有 2 个问题
- 该代码在您运行时是否返回任何错误?
- 为什么你有两种
increment!
方法? - 您
@requests
的 ERB 模板中有问题,因为您的控制器中没有@requests
实例变量。
.increment!
是正确的投票方法;但是,您需要保存它,因为
仅
attribute
更新;记录本身没有保存。
因此,您需要将记录保存到数据库
def upvote
@request = Request.find(params[:request_id])
@request.increment!(:voteCount)
@request.save
render json: { voteCount: @request.voteCount }
end
关于该主题的可能链接:
推荐阅读
- javascript - 电话号码的身份验证在 IOS 设备上不起作用
- excel - 基于另一个单元格的Excel过滤器公式如果为空则获取任何
- sas - 计算 SAS 可视化分析中行总数的百分比
- laravel - Blade UI Kit 的 ColorPicker:如何获取 Pickr 实例
- c - 带 void 指针参数的循环缓冲区
- javascript - TypeError:无法在 hanldeClick 处读取 null 的属性(读取“样式”)
- django - Django - 在查询集中计数和过滤
- java - 组合字符串及其反向的递归方法
- java - 如何将测试容器中的视频记录放置到 allure-report
- asp.net-mvc - 如何将数据从 DTO 映射到导航属性?