Rails 3资源路由缺少路径

时间:2011-08-18 22:03:32

标签: ruby-on-rails ruby-on-rails-3 routes

我不知道我用这个新应用程序做了什么,但我的路线真的搞砸了。我似乎错过了很多资源的路径,这对我的应用程序造成了严重破坏。有几个控制器似乎相互路由。我会发布我的路线文件和我的佣金路线来解释:

路由文件

devise_for :users
root :to => "index#home"

#members section and its nested images
resources :members, :except => [:new, :create] do
    resources :tattoos
end

#admin section
namespace :admin do
  root :to => "admin#index"

  # pulls all users to manage
  resources :users do
    #pulls just admin/artist users
    collection do
      get 'admins'
      get 'artists'
    end
  end

  # Adds resources to manage (approve/reject) images
  resources :tattoos do
    # adds a couple extra actions on images
    collection do
      get 'reported'
      put "mass_approve"
    end
  end
end


#public tattoo viewing and submissions
match "/submit" => "index#new", :via => :get, :as => "submit"
match "/submit" => "index#create", :via => :post
match "/tattoo/:id" => "index#show", :via => :get, :as =>"tattoo"
match "/tagged" => "index#tagged", :via => :get
match "/tattoo/:id" => "index#destroy", :via => :delete
match "/tattoos" => "index#index", :via => :get, :as => "tattoos"

佣金路线

                      root        /(.:format)                                    {:controller=>"index", :action=>"home"}
            member_tattoos GET    /members/:member_id/tattoos(.:format)          {:action=>"index", :controller=>"tattoos"}
                           POST   /members/:member_id/tattoos(.:format)          {:action=>"create", :controller=>"tattoos"}
         new_member_tattoo GET    /members/:member_id/tattoos/new(.:format)      {:action=>"new", :controller=>"tattoos"}
        edit_member_tattoo GET    /members/:member_id/tattoos/:id/edit(.:format) {:action=>"edit", :controller=>"tattoos"}
             member_tattoo GET    /members/:member_id/tattoos/:id(.:format)      {:action=>"show", :controller=>"tattoos"}
                           PUT    /members/:member_id/tattoos/:id(.:format)      {:action=>"update", :controller=>"tattoos"}
                           DELETE /members/:member_id/tattoos/:id(.:format)      {:action=>"destroy", :controller=>"tattoos"}
                   members GET    /members(.:format)                             {:action=>"index", :controller=>"members"}
               edit_member GET    /members/:id/edit(.:format)                    {:action=>"edit", :controller=>"members"}
                    member GET    /members/:id(.:format)                         {:action=>"show", :controller=>"members"}
                           PUT    /members/:id(.:format)                         {:action=>"update", :controller=>"members"}
                           DELETE /members/:id(.:format)                         {:action=>"destroy", :controller=>"members"}
                admin_root        /admin(.:format)                               {:controller=>"admin/admin", :action=>"index"}
        admins_admin_users GET    /admin/users/admins(.:format)                  {:action=>"admins", :controller=>"admin/users"}
       artists_admin_users GET    /admin/users/artists(.:format)                 {:action=>"artists", :controller=>"admin/users"}
               admin_users GET    /admin/users(.:format)                         {:action=>"index", :controller=>"admin/users"}
                           POST   /admin/users(.:format)                         {:action=>"create", :controller=>"admin/users"}
            new_admin_user GET    /admin/users/new(.:format)                     {:action=>"new", :controller=>"admin/users"}
           edit_admin_user GET    /admin/users/:id/edit(.:format)                {:action=>"edit", :controller=>"admin/users"}
                admin_user GET    /admin/users/:id(.:format)                     {:action=>"show", :controller=>"admin/users"}
                           PUT    /admin/users/:id(.:format)                     {:action=>"update", :controller=>"admin/users"}
                           DELETE /admin/users/:id(.:format)                     {:action=>"destroy", :controller=>"admin/users"}
    reported_admin_tattoos GET    /admin/tattoos/reported(.:format)              {:action=>"reported", :controller=>"admin/tattoos"}
mass_approve_admin_tattoos PUT    /admin/tattoos/mass_approve(.:format)          {:action=>"mass_approve", :controller=>"admin/tattoos"}
             admin_tattoos GET    /admin/tattoos(.:format)                       {:action=>"index", :controller=>"admin/tattoos"}
                           POST   /admin/tattoos(.:format)                       {:action=>"create", :controller=>"admin/tattoos"}
          new_admin_tattoo GET    /admin/tattoos/new(.:format)                   {:action=>"new", :controller=>"admin/tattoos"}
         edit_admin_tattoo GET    /admin/tattoos/:id/edit(.:format)              {:action=>"edit", :controller=>"admin/tattoos"}
              admin_tattoo GET    /admin/tattoos/:id(.:format)                   {:action=>"show", :controller=>"admin/tattoos"}
                           PUT    /admin/tattoos/:id(.:format)                   {:action=>"update", :controller=>"admin/tattoos"}
                           DELETE /admin/tattoos/:id(.:format)                   {:action=>"destroy", :controller=>"admin/tattoos"}
                    submit GET    /submit(.:format)                              {:controller=>"index", :action=>"new"}
                           POST   /submit(.:format)                              {:controller=>"index", :action=>"create"}
                    tattoo GET    /tattoo/:id(.:format)                          {:controller=>"index", :action=>"show"}
                    tagged GET    /tagged(.:format)                              {:controller=>"index", :action=>"tagged"}
                           DELETE /tattoo/:id(.:format)                          {:controller=>"index", :action=>"destroy"}
                   tattoos GET    /tattoos(.:format)                             {:controller=>"index", :action=>"index"}

现在,我遇到的问题是,如果我尝试将新纹身作为用户发布(这是我的嵌套资源的一部分),我会以某种方式路由到 / tattoos (在我的索引中)控制器)当我尝试创建记录时,我应该将其路由到 members / member_id / tattoos。如果我删除路线中的那一行(匹配“/ tattoos”=&gt ;“index #index”,:via =>:get,:as =>“tattoos ”并尝试转到 / members / member_id / tattoos / new 我得到了关注错误:

没有路线匹配“/ tattoos”

事实上,如果我尝试从/ submit页面上传新照片,我也会收到错误吗?

是什么给出的?我的路线完全糟糕!

要求良好衡量的表格代码和控制器代码

    -- Forms:
    <div class="content">
      <%= form_for @tattoo, :html =>{:multipart => true} do |f| %>
       <ol>
          <li> blah blah blah

--members>tattoos_controller
class TattoosController < ApplicationController

  def new
    @tattoo = Tattoo.new
  end

  def create
    @tattoo = Tattoo.new(params[:image])

    if @tattoo.save
      flash[:success] = "Tattoo sent in for approval!"
      redirect_to(member_tattoos_path(current_user, @tattoo))
      else
      render :action => "new"
    end
  end

class IndexController < ApplicationController

  def new
    @tattoo =Tattoo.new
  end

  def create
    @tattoo =Tattoo.new(params[:image])

    if @tattoo.save
      flash[:success] = "Tattoo sent in for approval!"
      redirect_to(tattoos_path)
      else
      render :action => "new"
    end
  end

2 个答案:

答案 0 :(得分:2)

我认为你的路线都是混乱的味道。对不起,我不知道如何涂糖,所以我没有。

为什么您的路线被称为会员?为什么不给用户打电话或重命名User模型而不是名为Member


解决方案#1

将您的路线更改为此(简化,根据需要进行调整):

resources :users do
  resources :tattoos
end

例如,路线将为/users/1/tattoos。然后,您就可以像这样使用form_for

<%= form_for [current_user, @tattoo], :html => { :multipart => true } %>

解决方案#2

保持路线不变,并将User模型重命名为Member。您需要重命名模型,表格,告诉Devise您需要devise_for :members而不是devise_for :users文件中的config/routes.rb以及我现在忘记的其他一些事情。


我想要了解的全部内容:请尽量使代码与应用程序的前端部分保持一致。当您将其称为“用户”时,您不希望让您的用户将自己称为“成员”。为了这样做,它几乎类似于说不同的语言。它只会导致心脏疼痛(疼痛)。

请一致地在任何地方命名

答案 1 :(得分:1)

在您的member_tattoos表单

上试试这个
<%= form_for [current_user, @tattoo], :html => { :multi_part => true } do |f| %>

如果您在member_tattoos页面上查看生成的表单操作,我打赌它是'/ tattoos'而不是'/ members /(无论id)/纹身'。