1

我的路线有一个谜团:

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"}

我从未请求过哪条路径。顺便说一句,存在哪条路径。

有什么东西可以拦截路线并在rails(或设计)中进行一些隐藏的重定向吗?

更新

以下是路线:

       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

更新

下面是 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

更新

这是该组织的 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 %>

更新

我还检查了路线是否定义明确,它们似乎是:

>> 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"}
4

2 回答 2

0

在您的“organisations/new.html.erb”标准中,创建了一个指向organisation_path(@organisation). 而那个不存在,因为你没有组织的表演。

更新:如果使用了路径助手,则错误指向那里,但link_to "Back", @organisation可能已使用...

于 2012-10-09T13:27:43.480 回答
0

您已将索引定义为

def index
  new 
  render 'organizations/new' 
end

如果您不指定要呈现的文件名,则呈现带有 method_name 的文件。因为你已经指定了组织/新的,我想你想显示新组织的表格你想重定向到新的组织表格吗?如果是,则添加

redirect_to new_organization_path and return

如果您想显示组织列表并在同一页面上接受新的组织数据,您可以这样做

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

并以你的新形式

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

它应该是organzations_path

于 2012-10-09T16:15:42.453 回答