Reputation: 43
I've got an issue using 'render' in Rails. I've got a custom action "search" in my controller, which is supposed to render index, as shown below.
def search
@date = params[:reserve_date]
@tables = Table.all
render 'index'
end
The action is being used in a different view using the following piece of code:
<%= form_with url: search_tables_path do |f| %>
<%= f.date_field :reserve_date %>
<%= submit_tag "SEND" %>
<% end %>
After pressing the 'SEND' button, the 'index' view should be rendered. The server states:
Rendered tables/index.html.erb within layouts/application (25.0ms)
Completed 200 OK in 127ms (Views: 123.2ms | ActiveRecord: 3.0ms)
However, nothing is being displayed in the browser at all. It stays at the previous page with no change. If I try to access the index view via a link like this:
Then the index view is being displayed normally, but of course I don't pass any extra information then. How can I have it render properly?
EDIT Form tag in HTML appears to be complete
<form action="/tables/search" accept-charset="UTF-8" data-remote="true" method="post"><input name="utf8" type="hidden" value="✓" /><input type="hidden" name="authenticity_token" value="4L/RNvCzlPX8PzDGk2LXcSiyrhzXVU3cwsLUmaQvq10y9HpESbH8CX+74n/UHxLJ5LUdiWYBdoaqCb2jFIYEgw==" />
This is my routes.rb file:
Rails.application.routes.draw do
devise_for :users
resources :tables do
collection do
post 'search'
end
end
resources :reservations
root 'home#index'
end
Upvotes: 4
Views: 2198
Reputation: 1459
Forms generated with form_with
by default has data-remote
set to true
.
If the data-remote
is set to true
your form makes an AJAX call. So your view is getting rendered as the response of that AJAX call. That is why you are not getting any errors.
Add local: true
in your form_with
You can learn more about form_with
from the docs.
Update:
As @julien-lamarche suggested in the comment, in the latest version of Rails, you may also need to add data: { turbo: false }
if you are using Turbo, which is the default case in the latest version of Rails.
Upvotes: 13