This is in application.html.erb
, and it works on 95% of the pages in my app:
<% ['usd', 'eur', 'aud'].each do |currency| %>
<%= form_with url: {controller: :home, action: :currency_select}, method: :post do |currency_form| %>
<%= currency_form.hidden_field :preferred_display_currency, value: currency %>
<%= currency_form.submit currency, class: "btn-info shadow-none" %>
<% end %>
<% end %>
But when I visit a certain view, before the page even loads, it gives this error:
ActionView::Template::Error (No route matches {:action=>"currency_select", :controller=>"users/home"}):
191:
192: <%= form_with url: {controller: :home, action: :currency_select}, method: :post do |currency_form| %>
193: <%= currency_form.hidden_field :preferred_display_currency, value: currency %>
194: <%= currency_form.submit currency, class: "btn-info shadow-none" %>
195: <% end %>
196:
197: <% end %>
I'm pretty sure something to do with :controller=>"users/home"
(where it should simply be :controller=>"home"
)
Why is the form suddenly confused about the controller?
Solution 1
Run
it returns
Now just use
url: currency_select_path
like so:Solution 2
Replace
:home
with"/home"
:(not totally sure why that works, but can confirm that it indeed works!)
A work around
The above two solutions are best, but a work around for the problem could be:
This way it simply avoids displaying the form on the route that errors. It's not ideal, but a practical work around.