代码之家  ›  专栏  ›  技术社区  ›  Yuki.M

方法为get的Rails form_无法更新视图

  •  1
  • Yuki.M  · 技术社区  · 6 年前

    尽管我按了按钮, enter image description here

    文本未更新。

    enter image description here

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

    用户控制器.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 helper方法呈现)时,文本将更新。

    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)
    
    2 回复  |  直到 6 年前
        1
  •  5
  •   Jay-Ar Polidario    6 年前

    你能试试下面的吗?

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

    说明:

    我注意到你的日志

    这意味着表格是与 format: :js form_with 现在默认为 remote: true HERE (你不能这样做 remote: false 但是,相反,使用 local: true form_for form_tag 哪里 默认情况下,但是您指定 远程:真

        2
  •  1
  •   Anand    6 年前
    <div><%= @message %></div>
    <%= form_tag(users_path, method: :get) do%>
        <%=submit_tag 'SUBMIT'%>
    <% end %>
    <%= link_to 'LINK', { :controller => :users, :action => :index } %>
    

    或者

    <%= form_with url: users_path, method: :get do |form| %>
      <%= form.submit 'SUBMIT'%>
    <%end%>
    <%= link_to 'LINK', { :controller => :users, :action => :index } %>