简体   繁体   English

将实例变量保存到数据库Rails

[英]Save instance variable to database Rails

I have been trying to save an instance variable and then increment a value on that instance variable, but it always resets when I reload. 我一直在尝试保存一个实例变量,然后在该实例变量上增加一个值,但是当我重新加载时它总是会重置。

Model looks like this 模型看起来像这样

class Waiting < ApplicationRecord
def increment
    self.number_waiting += 1
    save!
end
def decrement
    self.number_waiting -= 1
    save!
end
def reset
    self.number_waiting = 0
    save!
end
end

Controller looks like this 控制器看起来像这样

class IndexController < ApplicationController
def index
    @waiting ||= Waiting.new(number_waiting: 0)
    @waiting.increment
    @waiting.save!
end
end

Html looks like this: Html看起来像这样:

<p id="waiting_counter">
        <% @waiting.increment %>
        <%= @waiting.number_waiting %>
        <% @waiting.save! %>
    </p>

For some reason it is always at 2 and never changes... Been looking for a solution to this simple problem for several hours, please help. 由于某种原因,它始终是2,永远不会改变...一直在寻找解决这个简单的问题几个小时,请帮助。

Walkthrough your code and it should be obvious why it is always 2. 演练你的代码,它应该是显而易见的,为什么它总是2。

I assume you are navigating to IndexController#index (probably through indexes_path). 我假设您正在导航到IndexController#index #index(可能通过indices_path)。 This creates a completely new Waiting object initialised to 0 which is on the next line incremented by 1. Then you are saving it. 这会创建一个全新的Waiting对象,初始化为0,在下一行上加1,然后保存它。

Then (assuming that html is from index.html.erb) rails will render the html during which the @waiting object is incremented once more, before displaying the value which is now 2. Then that object is again saved. 然后(假设html来自index.html.erb)rails将呈现html,在此期间@waiting对象再次递增,然后显示现在为2的值。然后再次保存该对象。

It's behaving exactly as it should, however I think the real issue is that you may have a misconception about how rails works. 它的行为完全符合预期,但我认为真正的问题是你可能对rails如何运作产生误解。 When calling index action on the controller the result of calling that action (by default) is that the page index.html.erb will be rendered during which time the embedded ruby is evaluated. 在控制器上调用索引操作时,调用该操作的结果(默认情况下)是将呈现页面index.html.erb,在此期间将评估嵌入的ruby。 This all happens server side and then the resulting html is sent back to the client. 这一切都发生在服务器端,然后生成的html被发送回客户端。 You have nothing client side to initiate triggering the increment. 您没有任何客户端可以启动触发增量。

It's unclear what you are actually trying to achieve here, and in what way the increment is expected be invoked. 目前还不清楚你在这里想要实现什么,以及预期以何种方式调用增量。

After reviewing your question it has occurred to me that maybe you are expecting the instance variable to be available to all requests once initialised. 在审核了您的问题之后,我发现可能您希望实例变量在初始化后可用于所有请求。 That isn't the case - each request will get a new instance of the controller, so each time you reload the page @instance will initially be nil and will result in the creation of a new Waiting object. 事实并非如此 - 每个请求都会获得一个新的控制器实例,因此每次重新加载页面时,@ instance最初都将为nil,并将导致创建一个新的Waiting对象。 What you should do instead is grab the Waiting object from the database and then increment that, eg: 你应该做的是从数据库中获取Waiting对象然后递增,例如:

def index
    @waiting = Waiting.first
    @waiting ||= Waiting.new(number_waiting: 0)
    @waiting.increment
    @waiting.save!
end

Incidentally by convention you would normally display a list of existing objects through index action. 顺便提一下,按照惯例,您通常会通过索引操作显示现有对象的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM