I want user to work with only one order connected to user's session. So I set singular resource for order
routes.rb:
resource :order
views/orders/new.html.erb:
<%= form_for @order do |f| %>
...
<% end %>
But when I open the new order page I get an error:
undefined method `orders_path`
I know, that I can set :url => order_path
in form_for
, but what is the true way of resolving this collision?
Unfortunately, this is a bug. You'll have to set the url like you mention.
Where does that magic path come from?
It took me a lot of tracing but I ultimately found that the url_for determines the path for your model using the
polymorphic_path
method defined in ActionDispatch::Routing::PolymorphicRoutes.polymorphic_path
ultimately gets the automagic path for your model by calling something along the lines of:I'm simplifying slightly but this is basically it. If you have an array (e.g.
form_for[@order, @item]
) the above is called on each element and the results are joined with_
.The
model_name
method on your Class comes from ActiveRecord::Naming.How can I change it?
Fortunately ActiveModel::Name precalculates all values including route_key, so to override that value all we have to do is change the value of the instance variable.
For the
:order
resource in your question:Try it out!