Rails 3 routes and modules

拥有回忆 提交于 2019-12-12 21:07:42

问题


I have a AR model inside a module

class Long::Module::Path::Model < ActiveRecord::Base
end

and want to use following routes (without the module names, because it's easier to write and remember)

resources :models

buts Rails 3 always wants to use a URL like

long_module_path_model_url

Is there are way to change this behavior?

Hope anyone out there can help me?

Mario


回答1:


I'm a little curious why you're referencing a model when talking about routing which only handles the controller level; but this article should be helpful: R3 Controller Namespaces and Routing

"If you want to route /photos (without the prefix /admin) to Admin::PostsController, you could use:

scope :module => "admin" do
  resources :posts, :comments
end

"

If you'd like the named paths to change, you can use :as, as specified here: R3 Prefixing the Named Routes Helpers

So I'm guessing something along the lines of

1:

scope :module => 'long/module/path' do
   resources :model, :as => :model
end

or 2:

scope :module => 'long' do
  scope :module => 'module' do 
   scope :module => 'path' do
    resources :model, :as => :model
   end end end

Is what you're looking for.




回答2:


resources :your_looooooong_model_name, :as => :short

Would give you shorts_url, etc.




回答3:


I know this is an old question, but the others misunderstood your question and didn't solve your problem.

You need to override the model_name method as below:

class Long::Module::Path::Model < ActiveRecord::Base
  def self.model_name
    ActiveModel::Name.new(Long::Module::Path::Model, nil, "YourNewModelName")
  end
end

Credit goes to this comment.



来源:https://stackoverflow.com/questions/3062704/rails-3-routes-and-modules

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