Ruby on rails 在RubyonRails中使用Ajax更改本地Ruby变量

Ruby on rails 在RubyonRails中使用Ajax更改本地Ruby变量,ruby-on-rails,ruby,ajax,controller,weekday,Ruby On Rails,Ruby,Ajax,Controller,Weekday,我正在创建一个显示每周计划的计划页面,并希望在页面底部制作箭头,在一周内前后切换。为此,我在控制器中创建了一个变量@wbeg,该变量默认为一周的开始和两个动作,一周之后和一周之前,以及相应的js.erb文件。我不知道如何使用ajax更改@wbeg变量,需要这样做才能使所有内容保持在同一页面上 这是控制器: class HomeController < ApplicationController def home end def scheduling

我正在创建一个显示每周计划的计划页面,并希望在页面底部制作箭头,在一周内前后切换。为此,我在控制器中创建了一个变量@wbeg,该变量默认为一周的开始和两个动作,一周之后和一周之前,以及相应的js.erb文件。我不知道如何使用ajax更改@wbeg变量,需要这样做才能使所有内容保持在同一页面上

这是控制器:

class HomeController < ApplicationController

    def home
    end

    def scheduling
        @work_days = WorkDay.all
        @jobs = Job.all
        @wbeg = Date.today - Date.today.wday + 1 #beginning of this week
    end

    def week_before
        respond_to do |format|
            format.html { redirect_to scheduling_path notice: "You should not see this message, contact admin if you do."}
            format.js
        end
    end

    def week_after
        respond_to do |format|
            format.html { redirect_to scheduling_path notice: "You should not see this message, contact admin if you do."}
            format.js
        end
    end

end
class HomeController
计划页面:

<p id="notice"><%= notice %></p>

<h1>Work Day Scheduling</h1>

<table>
    <thead>
        <tr>
            <th>Job #</th>
            <th>Job</th>
            <th>Monday</th>
            <th>Tuesday</th>
            <th>Wednesday</th>
            <th>Thursday</th>
            <th>Friday</th>
            <th>Saturday</th>
        </tr>
    </thead>
    <tbody id = "main_table">
        <%= render "home/main_table" %>
    </tbody>
</table>
<%= link_to '<--', week_before_path, remote: true %>
<%= link_to '-->', week_after_path, remote: true %>

工作日安排 工作# 工作 星期一 星期二 星期三 星期四 星期五 星期六
以及两个js.erb页面:

$('#main_table').html("<%= j render 'main_table', locals: { wbeg: @wbeg - 7 } %>");
$('#main_table').html(“”);
另一个:

$('#main_table').html("<%= j render 'main_table', locals: { wbeg: @wbeg + 7 } %>");
$('#main_table').html(“”);

我还尝试在控制器中的操作前后的一周内更改变量,但它会给出相同的错误“nil cannot have operation”-”或其他什么,这意味着它无法识别变量

按照现在的编码方式,@wbeg变量在生成javascript文件时的值将硬编码到javascript文件中。这个价值永远不会改变。不管javascript生成时是什么,都是它


您需要的是一个javascript变量,可以在进行AJAX调用的javascript代码中进行更新。

我不知道如何更新和处理javascript变量,因为我需要能够将@wbeg变量一次移动7天,并将其转换为javascript,从而将变量转换为字符串,而不是日期用红宝石做的。不过,我最终通过使用RubyonRails会话解决了这个问题。谢谢你的帮助。