2012-10-09 97 views
1

我在我的路线是一个谜:资源路由的mixup

resources :organizations 

root :to => 'users#index' 

devise_for :users, :controllers => { :registrations => 'users/registration', :sessions => 'users/session', :confirmations => 'users/confirmation'} 

现在,如果我尝试达到/organizations/1它的工作原理。

如果我尝试/organizations/organizations/new

我得到同样的错误,对于两种情况:No route matches {:action=>"show", :controller=>"organizations"}

哪条路我从来没有要求。以及哪个路径存在btw。

有什么可以拦截路线并在轨道(或设计)中做一些隐藏的重定向?

UPDATE

这里是路线:

 organizations GET /organizations(.:format)    organizations#index 
        POST /organizations(.:format)    organizations#create 
    new_organization GET /organizations/new(.:format)   organizations#new 
    edit_organization GET /organizations/:id/edit(.:format)  organizations#edit 
     organization GET /organizations/:id(.:format)   organizations#show 
        PUT /organizations/:id(.:format)   organizations#update 
        DELETE /organizations/:id(.:format)   organizations#destroy 
       root  /         users#index 
    new_user_session GET /users/sign_in(.:format)    users/session#new 
     user_session POST /users/sign_in(.:format)    users/session#create 
destroy_user_session DELETE /users/sign_out(.:format)    users/session#destroy 
     user_password POST /users/password(.:format)    devise/passwords#create 
    new_user_password GET /users/password/new(.:format)   devise/passwords#new 
    edit_user_password GET /users/password/edit(.:format)  devise/passwords#edit 
        PUT /users/password(.:format)    devise/passwords#update 
cancel_user_registration GET /users/cancel(.:format)    users/registration#cancel 
     user_registration POST /users(.:format)      users/registration#create 
    new_user_registration GET /users/sign_up(.:format)    users/registration#new 
    edit_user_registration GET /users/edit(.:format)     users/registration#edit 
         PUT /users(.:format)      users/registration#update 
         DELETE /users(.:format)      users/registration#destroy 
     user_confirmation POST /users/confirmation(.:format)   users/confirmation#create 
    new_user_confirmation GET /users/confirmation/new(.:format)  users/confirmation#new 
         GET /users/confirmation(.:format)   users/confirmation#show 
      user_unlock POST /users/unlock(.:format)    devise/unlocks#create 
     new_user_unlock GET /users/unlock/new(.:format)   devise/unlocks#new 
         GET /users/unlock(.:format)    devise/unlocks#show 

UPDATE

这里有OrganizationsController和ApplicationController中:

class OrganizationsController < ApplicationController 
    before_filter :authenticate_user! 

    def index 
    new 
    render 'organizations/new' 
    end 

    def new 
    @organization = Organization.new 
    end 
end 

class ApplicationController < ActionController::Base 
    protect_from_forgery 

    def after_sign_in_path_for(resource) 
    accounts_path 
    end 

    rescue_from CanCan::AccessDenied do |exception| 
    flash[:error] = exception.message 
    redirect_to :back 
    end 
end 

UPDATE

这里是该组织的new.html.erb观点

<%= semantic_form_for @organization, :url => organization_path do |f| %> 

    <%= f.inputs do %> 
     <%= f.input :country %> 
     <%= f.input :type, :as => :select, :label => t(:g_type), :collection =>  [[t(:g_company),"Company"],[t(:g_person),"Person"]] %> 
    <% end %> 
    <%= f.actions %> 
<% end %> 

UPDATE

我也查了路线明确界定,并他们似乎是:

>> r = Rails.application.routes 
#<ActionDispatch::Routing::RouteSet:0x3dbea00> 
>> r.recognize_path("/organizations/new") 
{:action=>"new", :controller=>"organizations"} 
>> r.recognize_path("/organizations/1") 
{:action=>"show", :controller=>"organizations", :id=>"1"} 
+0

你能后的'耙路线'的结果? –

+0

当然,只是将它添加到主帖子。 – muichkine

+0

路由看起来很好,你重启了你的服务器吗? –

回答

0

在您的“organizations/new.html.erb”标准中,链接创建为organisation_path(@organisation)。而那个不存在,因为你没有为组织展示。

更新:如果使用的路径帮手,错误指着那里,但problably link_to "Back", @organisation使用...

+0

我曾尝试在我的组织控制器中添加一个“show”动作,错误仍然显示出来,现在,redirect_to:back由CanCan触发:: AccessDenied异常,为什么会被调用? – muichkine

+0

我在'rescue_from CanCan :: AccessDenied'中添加了一个breakboint,并且在请求'/ organizations/new'时从未触发。 – muichkine

+0

1.首先删除CanCan函数,以查看是否这是导致它2.发布你的new.html.erb,检查是否有错误的链接 –

0

u必须定义指数

def index 
    new 
    render 'organizations/new' 
end 

如果u不指定文件要渲染的名称,将渲染带有method_name的文件。既然你已经指定组织/新,我想你想要显示新组织形式 你想重定向到新的组织形式吗?如果是,再加入

redirect_to new_organization_path and return 

如果妳想要显示的组织名单,并接受相同的页面上的新的组织数据,U可以这样做

def new 
    @organizations = Organization.all 
    @organization = Organization.new 
end 

,并在乌拉圭回合新形式

= form_for(@organization, :url => organizations_path, :method => :post) do |f| 

应该organzations_path

+0

那么,我不明白这与路线和演出方法有什么关系。你所描述的是我遵循的标准rails方式(我不想重定向我想要在我的索引操作中呈现)。此外,我没有得到你在最后一篇文章中说的话:为什么我应该拼写** organizations_path **作为** organzations_path ** ?! – muichkine

+0

它应该是organizational_path而不是organization_path。复数。 –

+0

好吧,我从new.html.erb中删除了整个代码,问题仍然存在。不知道我还能做些什么...我真的被困在这里。 – muichkine