I'm working on a rails project and I'm stuck because rails doesn't render a view despite in console appeare like rendered. I have this form in index.html.erb of Trip scaffold
<%= form_with(url: filter_path, method: "get") do |form| %>
<div class="field">
<%= form.label :from %>
<%= form.select :from, @places_arr %>
</div>
<div >
<%= form.label :to %>
<%= form.select :to, @places_arr %>
</div>
<div class="field">
<%= form.label :departure_date %>
<%= form.date_select :dep_date %>
</div>
<div >
<%= form.label :departure_time %>
<%= form.text_field :dep_time %>
</div>
<div class="field">
<%= form.label :price %>
<%= form.text_field :price %>
</div>
<div >
<%= form.label :rating %>
<%= form.text_field :rating %>
</div>
<div class="field">
<%= form.label :model %>
<%= form.select :model, @vehicles_arr %>  
</div>
<div >
<%= form.label :alimentation %>
<%= form.select :alimentation, @vehicles_alimentation %>
</div>
<div class="btn btn-primary">
<%= form.button :submit %>
</div>
<% end %>
This is the route that redirect to the action:
get 'trips/filter', to: 'trips#filter', as: 'filter'
Action "filter" in Controller is actually empty
def filter
end
I think when I click on "Submit" on form I should be redirected to the view filter.html.erb, but this doesn't happens. Nothing happens.
In console appears this:
Started GET "/trips/filter?from=7&to=7&[dep_date(1i)]=2021&[dep_date(2i)]=11&[dep_date(3i)]=23&dep_time=&price=&rating=&model=&alimentation=benzina&button=" for 127.0.0.1 at 2021-11-23 19:24:55 0100
Processing by TripsController#filter as JS
Parameters: {"from"=>"7", "to"=>"7", "dep_date(1i)"=>"2021", "dep_date(2i)"=>"11", "dep_date(3i)"=>"23", "dep_time"=>"", "price"=>"", "rating"=>"", "model"=>"", "alimentation"=>"benzina", "button"=>""}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ app/controllers/application_controller.rb:8:in `current_user'
Rendering layout layouts/application.html.erb
Rendering trips/filter.html.erb within layouts/application
Rendered trips/filter.html.erb within layouts/application (Duration: 0.0ms | Allocations: 4)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layouts/_flash.html.erb (Duration: 0.0ms | Allocations: 12)
CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ app/controllers/application_controller.rb:8:in `current_user'
CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ app/controllers/application_controller.rb:8:in `current_user'
CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ app/controllers/application_controller.rb:8:in `current_user'
Rendered layouts/_header.html.erb (Duration: 2.5ms | Allocations: 1458)
Rendered layout layouts/application.html.erb (Duration: 15.0ms | Allocations: 10550)
Completed 200 OK in 18ms (Views: 15.5ms | ActiveRecord: 0.4ms | Allocations: 11518)
Can someone help me?
CodePudding user response:
Prior to Rails 6.1 form_with
defaults to remote: true
and will send an XHR request for JS. You can see that thats what happening from:
Processing by TripsController#filter as JS
Rails won't actually raise if the js.erb
template is missing and will just implicitly render the HTML template which is sent back to the browser which does absolutely nothing with it.
If you want to send a normal syncronous request you need to explicitly set it in the form:
<%= form_with(url: filter_path, method: "get", remote: false) do |form| %>
If you want to buy into the whole Server Side Concerns gimmick you need to create a js.erb
template.
# filter.js.erb
document.getElementById("#myid")
.innerHTML = "<%= j render partial: 'some_partial' %>";
However Rails UJS is (finally) dead as of Rails 7 so its not a very future proof approach.