代码之家  ›  专栏  ›  技术社区  ›  nlaq

ruby on rails模型和不同名称空间中的控制器

  •  1
  • nlaq  · 技术社区  · 16 年前

    好啊。这太疯狂了。

    我对ror还不熟悉,我真的很想了解它,因为到目前为止我所看到的一切都使它对我所做的工作更具吸引力。

    然而,我似乎无法完成 非常 关于罗的简单事情。

    我要这些控制器:

    /admin/blog/entries (index/show/edit/delete)
    /admin/blog/categories (index/show/edit/delete)
    /admin/blog/comments (index/show/edit/delete)
    ... and so on
    

    这些模型:

    Blog::Entry    (table: blog_entries)
    Blog::Category (table: blog_categories)
    Blog::Comments (table: blog_comments)
    ... and so on
    

    现在,我已经经历了相当多的痛苦,使这项工作。我的第一次尝试是生成脚手架(我使用2.2.2)。我生成了我的脚手架,但是必须移动我的模型,然后在我的控制器中修复对模型的引用(参见 Ruby on Rails model inside namespace can't be found in controller )

    这已经是一个很大的痛苦,但嘿,我有它的工作。现在,虽然form_for不起作用,我也不知道如何使用url帮助程序(我不知道这些被称为…它们是自动生成的方法,将url返回给与模型关联的控制器。我不知道他们叫什么名字。我的模型是blog::entries。我试着搞乱路线。rb地图的资源方法,但是没有运气。当我试图将form_用于我的模型时,我得到了这个错误

    undefined method `blog_entries_path' for #<ActionView::Base:0xb6848080>
    

    现在。这真的很令人沮丧。为了使用这个框架,我不会完全破坏代码的组织,如果我不知道如何完成这个简单的任务(我已经研究了至少5个小时),那么我就不能继续了。

    有什么办法可以做到这一点吗?

    谢谢

    编辑

    以下是我的路线:

                 admin_blog_entries GET    /admin_blog_entries                  {:controller=>"admin_blog_entries", :action=>"index"}
       formatted_admin_blog_entries GET    /admin_blog_entries.:format          {:controller=>"admin_blog_entries", :action=>"index"}
                                    POST   /admin_blog_entries                  {:controller=>"admin_blog_entries", :action=>"create"}
                                    POST   /admin_blog_entries.:format          {:controller=>"admin_blog_entries", :action=>"create"}
               new_admin_blog_entry GET    /admin_blog_entries/new              {:controller=>"admin_blog_entries", :action=>"new"}
     formatted_new_admin_blog_entry GET    /admin_blog_entries/new.:format      {:controller=>"admin_blog_entries", :action=>"new"}
              edit_admin_blog_entry GET    /admin_blog_entries/:id/edit         {:controller=>"admin_blog_entries", :action=>"edit"}
    formatted_edit_admin_blog_entry GET    /admin_blog_entries/:id/edit.:format {:controller=>"admin_blog_entries", :action=>"edit"}
                   admin_blog_entry GET    /admin_blog_entries/:id              {:controller=>"admin_blog_entries", :action=>"show"}
         formatted_admin_blog_entry GET    /admin_blog_entries/:id.:format      {:controller=>"admin_blog_entries", :action=>"show"}
                                    PUT    /admin_blog_entries/:id              {:controller=>"admin_blog_entries", :action=>"update"}
                                    PUT    /admin_blog_entries/:id.:format      {:controller=>"admin_blog_entries", :action=>"update"}
                                    DELETE /admin_blog_entries/:id              {:controller=>"admin_blog_entries", :action=>"destroy"}
                                    DELETE /admin_blog_entries/:id.:format      {:controller=>"admin_blog_entries", :action=>"destroy"}
                               home        /                                    {:action=>"index", :controller=>"index"}
                                           /:controller/:action/:id
                                           /:controller/:action/:id.:format
    

    看起来不对。这是my routes.rb(删除注释):

    ActionController::Routing::Routes.draw do |map|
    
      map.resources :admin_blog_entries
    
      map.home '', :controller => 'index'
    
      map.connect ':controller/:action/:id'
      map.connect ':controller/:action/:id.:format'
    end
    
    3 回复  |  直到 10 年前
        1
  •  2
  •   Maximiliano Guzman    16 年前

    你试过看“rake routes”给你的路由列表吗?如果routes.rb正确,它应该显示博客条目路由的正确名称。

    此外,也许这可以帮助: http://www.coreywoodcox.com/2008/08/18/rails-namespaces-subdomains/ .

    编辑:

    那么,调用路由的正确方法是admin_blog_entries_path,而不是blog_entries_path。

        2
  •  1
  •   klew    16 年前

    routes.rb应该如下所示:

    map.namespace :admin do |admin|
      admin.namespace :blog do |blog|
        blog.resources :entries
        blog.resources :categories
        ...
      end
    end
    

    但我不知道如何处理url中的“/blog/”部分(我的模型中还没有使用任何命名空间)。但通过这些路线,您将能够使用:

    admin_blog_categories_path               => '/admin/blog/categiries'
    admin_blog_category_path(@some_category) => '/admin/blog/categories/1'
    

    等等。

        3
  •  1
  •   Danny Hawkins    15 年前

    好吧,这是我相当老套的做法,我不喜欢,但确实有效。

    在我的例子中,我有模型blog::article,blog::comment,它们嵌套在路由中。使用这种方法的一个警告是:在博客::在加载文章时,可以期待PARAM[[ ToeLo.Id] ]或PARAM[[ BLOGJOTHOLYLY-ID] ]。一点也不好,但就像我说的。确实有效:/

    blog.resources :articles do |article|
      article.resources :comments
    end
    
    blog.resources :blog_articles, :controller => 'articles' do |blog_article|
      blog_article.resources :blog_comments, :controller => 'comments'
    end