gpt4 book ai didi

javascript - rails : Remote form in popup window not calling controller action

转载 作者:行者123 更新时间:2023-11-28 06:57:58 24 4
gpt4 key购买 nike

我正在编写一个时间跟踪应用程序。我有一个具有 time_logged 属性的 Project 模型。

我正在使用远程表单(在弹出窗口中呈现)来记录用户的时间。提交时,jQuery 事件处理程序将跟踪的时间(以秒为单位)分配给表单中的隐藏字段。所有 JS 都正确执行。

Controller 应该调用一个方法来记录针对表单中所选项目跟踪的时间,但项目永远不会更新。我认为要么是表单没有正确调用 Controller 操作,要么是我编写 Controller 的方式有问题。

但我不明白是什么。我将在下面包含相关的代码片段。

projects_controller.rb

respond_to :html, :js

def track
@project = Project.new
end

def log_time
@project = Project.find(params[:project][:id])
@project.increment_time_logged(project_log_time_params.to_i)
end

private

def project_params
params.require(:project).permit(:id, :name, :fee, :client_id)
end

def project_log_time_params
params.require(:project).permit(:time_logged)
end

项目.rb

  def increment_time_logged(number_of_seconds)
self.time_logged += number_of_seconds
end

routes.rb

  match '/projects/track-time', to: 'projects#track', via: 'get'
match '/projects/log-time', to: 'projects#log_time', via: 'put'

_track_time_form.html.erb

<%= javascript_include_tag "track.js.erb" %>
<div id="countdown-timer"></div>
<div id="playback-button">&#9658;</div>
<div id="track-time-form">
<%= form_for @project, :url => "/projects/log-time", remote: true do |p| %>
<ul>
<li><%= p.label :project, "Project:"%><br>
<%= p.collection_select(:id, current_user.projects, :id, :name) %></li>

<%= p.hidden_field :time_logged, :value => 0 %> <!-- value set by script in log_time.js.erb -->

<li><%= p.submit "Log time", id: "log-time-button" %></li>
</ul>
<% end %>
</div>

track.js.erb

//initialise form 
timeTrackingForm = window.open("", "", "height=700,width=500");
$(timeTrackingForm.document.body).html("<%= j render( :partial => 'track_time_form' ) %>");

//assign variables
var timer = $("#countdown-timer", $(timeTrackingForm.document));
var playbackControls = $("#playback-button", $(timeTrackingForm.document));
var form = $("#track-time-form", $(timeTrackingForm.document));
var actual_form = $("#new_project", $(timeTrackingForm.document));
var formUl = $("#track-time-form ul", $(timeTrackingForm.document));
var formLi = $("#track-time-form li", $(timeTrackingForm.document));
var logTimeButton = $("#log-time-button", $(timeTrackingForm.document));
var hidden = $("input:hidden", $(timeTrackingForm.document));
var timerPaused;

//initialise timer
$(timeTrackingForm.document).ready(function(){
initialiseTimer();
style();
$(playbackControls).click(function() {
playOrPause();
});
$(actual_form).submit(function(){
$(timer).timer('pause');
timerPaused = true;
var secondsTracked = $(timer).data('seconds');
$(hidden).val(secondsTracked);
resetTimerDisplay();
});
});

function initialiseTimer() {
$(timer).timer({
format: '%H:%M:%S'
});
$(timer).timer('pause');
timerPaused = true;
}

function resetTimerDisplay() {
$(timer).timer('reset');
}

function style() {
$(timer).css({'color':'black','font-size':'50px', 'margin':'auto', 'width':'180px'});
$(playbackControls).css({'color':'#290052', 'font-size':'50px', 'margin':'auto', 'width':'55px'});
$(form).css({'width':'300px','margin':'auto'});
$(formUl).css({'list-style-type':'none'});
$(formLi).css({'margin':'0 0 25px 0','font-sizeL':'18px','font-family':'Arial'});
$(logTimeButton).css({'width':'180px','font-size':'18px','background-color':'green','color':'white','margin-top':'15px'});
}

function playOrPause() {
if (timerPaused == true) {
$(timer).timer('resume');
timerPaused = false;
}
else {
$(timer).timer('pause')
timerPaused = true;
}
}

最佳答案

您的模型似乎有问题。目前您只是更改属性,而不是将记录保存到数据库中。

改变这个

 def increment_time_logged(number_of_seconds)
self.time_logged += number_of_seconds
end

对此:

 def increment_time_logged(number_of_seconds)
self.time_logged += number_of_seconds
self.save
end

或者,如果您想跳过事件记录验证,请使用:

 def increment_time_logged(number_of_seconds)
time = self.time_logged += number_of_seconds
self.update_attribute(:time_logged, time)
end

关于javascript - rails : Remote form in popup window not calling controller action,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32415556/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com