gpt4 book ai didi

ruby-on-rails - Rails form_with 方法 : get cannot update a view

转载 作者:行者123 更新时间:2023-12-04 06:04:32 25 4
gpt4 key购买 nike

使用 rails5 的 form_with,我想通过单击 GET 按钮来显示当前时间文本。但是文字没有更新。

虽然我按下了按钮,
enter image description here

文字未更新。

enter image description here

响应文本已更新。
enter image description here

users_controller.rb 正在关注。

require 'date'
class UsersController < ApplicationController
def index
@message = Time.now.to_s
end
end

index.html.erb 如下。
<div><%= @message %></div>
<%= form_with method: :get, url: users_path do |form| %>
<%= form.submit 'SUBMIT'%>
<% end %>
<%= link_to 'LINK', { :controller => :users, :action => :index } %>

请告诉我如何按当前时间更新文本。

注意:

当按下一个链接(由 link_to 辅助方法渲染)时,文本会更新。

按链接...

enter image description here

然后文本被更新。

enter image description here

为什么我可以通过链接更新文本?

编辑:

单击按钮时的服务器日志如下。
Started GET "/users?utf8=%E2%9C%93&commit=SUBMIT" for 127.0.0.1 at 2018-08-17 19:13:32 +0900
Processing by UsersController#index as JS
Parameters: {"utf8"=>"✓", "commit"=>"SUBMIT"}
Rendering users/index.html.erb within layouts/application
Rendered users/index.html.erb within layouts/application (4.7ms)
Completed 200 OK in 153ms (Views: 112.5ms | ActiveRecord: 0.0ms)

最佳答案

您可以尝试以下方法吗?

<%= form_with method: :get, url: users_path, local: true do |form| %>

解释:

我在你的日志中注意到

Processing by UsersController#index as JS



这意味着表单是用 format: :js 提交的因为 form_with现在默认为 remote: true如您所见 HERE (你不能做 remote: false 但是,而是使用 local: true ),不像 form_forform_tag哪里 remote: false默认情况下,但您指定 remote: true .

关于ruby-on-rails - Rails form_with 方法 : get cannot update a view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51892516/

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