Ruby on rails: singular resource and form_for

纵然是瞬间 提交于 2019-11-28 05:11:15
mckeed

Unfortunately, this is a bug. You'll have to set the url like you mention.

= form_for @order, :url => orders_path do |f|

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:

record.class.model_name.route_key

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.

module ActiveModel
  ...
  module Naming
    ...
    def model_name
      @_model_name ||= begin
        namespace = self.parents.detect do |n|
          n.respond_to?(:use_relative_model_naming?) && 
                                                 n.use_relative_model_naming?
        end
        ActiveModel::Name.new(self, namespace)
      end
    end
  end
end


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:

class Order < ActiveRecord::Base
  model_name.instance_variable_set(:@route_key, 'order')
  ...
end

# new.html.erb
<%= form_for @order do |f| # Works with action="/order" %>
    ...
<% end %>

Try it out!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!