Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails form_with method: get cannot update a view

Using form_with of rails5, I want to show current time text by clicking GET button. But text isn't updated.

Though I press the button, enter image description here

Text isn't updated.

enter image description here

Text in response is updated. enter image description here

users_controller.rb is following.

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

index.html.erb is following.

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

Please, tell me how to update text by current time.

NOTE:

When press a link(rendered by link_to helper method), text is updated.

press link...

enter image description here

then text is updated.

enter image description here

why I can update text by link?

EDIT:

Server log when clicking the button is following.

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)
like image 854
Yuki.M Avatar asked Dec 18 '22 21:12

Yuki.M


1 Answers

Can you try the following?

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

Explanation:

I noticed in your logs

Processing by UsersController#index as JS

which means that the form is submitted with format: :js because form_with now defaults to remote: true as you could see HERE (you can't do remote: false however, but instead use local: true), unlike form_for and form_tag where remote: false by default, but you specify remote: true.

like image 96
Jay-Ar Polidario Avatar answered Jan 01 '23 03:01

Jay-Ar Polidario