rails 3中非常奇怪的路由错误

时间:2013-08-14 07:06:14

标签: ruby-on-rails-3 templates rails-routing

我有一个名为MeController的控制器。它包含许多操作,例如spacesprojects

spaces操作执行以下操作:

  def spaces
    @spaces = current_user.spaces

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @note }
    end
  end

该视图只呈现另一个索引模板,如:

<%= render :template => "spaces/index" %> 

这完美无缺,但现在stränge部分开始......:S

这是project操作:

  def projects
    @projects = current_user.projects

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @projects }
    end
  end

该视图还呈现索引模板:

<%= render :template => "projects/index" %> 

但对于这些项目,我收到以下错误:

Routing Error

No route matches {:action=>"edit", :controller=>"spaces", :id=>nil}

我只是不明白为什么它应该是动作编辑和控制器空间的常规错误。 :/

以下是我的路线:

# Profiles & Current User
  resources :profiles

  get "me" => "profiles#show", :as => "current_user"
  get "me/spaces", :as => "current_user_spaces"
  get "me/projects", :as => "current_user_projects"
  get "me/tasks", :as => "current_user_tasks"
  get "me/notes", :as => "current_user_notes"
  get "me/discussions", :as => "current_user_discussions"
  get "me/files", :as => "current_user_files"

  # Permissions
  resources :permissions

  resources :spaces do
    resources :projects do
      resources :tasks
      resources :notes
    end
  end

  devise_for :users

  root :to => redirect("/me/spaces")

希望有人对我有所暗示! :)

1 个答案:

答案 0 :(得分:1)

所以我的猜测是:

在您的模板(项目/索引)中,您正在使用url_forlink_to等网址帮助程序以及指向特定项目的链接。问题是项目在您的space资源中嵌套了路由。这就是为什么当你希望它为项目生成一个url时,你必须为任何url helper提供对空间和项目的引用。

如果url帮助器不知道如何构造url,也会抛出RoutingError。

这是一个很长的镜头,但我希望它有所帮助。

相关问题