控制器上的路由问题具有不规则的复数

时间:2016-02-23 03:59:24

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

应用程序BusinessSessionsController处理Businesses控制器和Business模型的登录和登录会话。成功登录后,用户应路由到.../businesses/4,其中“4”是相应业务的ID。而是rails构建以下URL .../business.4

我的猜测是,这可能与“商业”这个词的不规则多元化有关。当我为users控制器运行相同的代码user模型,即常规复数时,它的工作正常。

我尝试设置一个不规则的变形器,如下所示,但添加它没有任何区别。 http://guides.rubyonrails.org/routing.html#naming-routes第4.8节

config/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
 inflect.irregular 'business', 'businesses'
end

电流控制器:

class BusinessSessionsController < ApplicationController

def create
  business = Business.find_by(email: params[:business_session][:email].downcase)
  if business && business.authenticate(params[:business_session][:password])
    business_log_in business
    redirect_to business
  else
    flash.now[:danger] = 'Invalid email/password combination'
    redirect_to business_url
  end  
end

end  

路线

get 'business' => 'businesses#new' #this page has the login form
get 'business_login' => 'business_sessions#new'
post 'business_login' =>  'business_sessions#create'
resources :businesses

1 个答案:

答案 0 :(得分:0)

你可以去除不规则的拐点,导轨可以正确处理。我创建了一个只有<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div> <div ng-app> {{4+5}} <!-- inside of ng-app block --> </div> <div> {{4+5}} <!-- outside of ng-app block --> </div> </div>的routes.rb,运行resources :business产生:

rake routes

但是你之前的定义是

       Prefix Verb   URI Pattern                    Controller#Action
   businesses GET    /businesses(.:format)          businesses#index
              POST   /businesses(.:format)          businesses#create
 new_business GET    /businesses/new(.:format)      businesses#new
edit_business GET    /businesses/:id/edit(.:format) businesses#edit
     business GET    /businesses/:id(.:format)      businesses#show
              PATCH  /businesses/:id(.:format)      businesses#update
              PUT    /businesses/:id(.:format)      businesses#update
              DELETE /businesses/:id(.:format)      businesses#destroy

会导致麻烦,因为这会生成一个名为get 'business' => 'businesses#new' #this page has the login form 的助手:

business_url

不同之处在于,一名助手有一个ID,而另一名助手没有,因为铁路&#39;惯例,优先级基于创建顺序:首先创建 - &gt;最高优先级 - 没有ID的人获胜。

如果您致电 Prefix Verb URI Pattern Controller#Action business GET /business(.:format) businesses#new ,会发生的情况是,调用没有ID的redirect to: business助手,并将商家的ID视为&#34;格式&#34;。

TL; DR解决此问题的两种方法:

1)这种方法很简单,就是删除business_url路由,然后使用get 'business' => 'businesses#new'帮助程序。然后,网址将显示为new_business_url

2)你真的希望网址为/businesses/new,然后你可以做一些诡计:

/business

这将产生以下路线:

  get 'business' => 'businesses#new', as: :new_business #this page has the login form
  resources :businesses, except: [:new] 

您仍然需要将 Prefix Verb URI Pattern Controller#Action new_business GET /business(.:format) businesses#new businesses GET /businesses(.:format) businesses#index POST /businesses(.:format) businesses#create edit_business GET /businesses/:id/edit(.:format) businesses#edit business GET /businesses/:id(.:format) businesses#show PATCH /businesses/:id(.:format) businesses#update PUT /businesses/:id(.:format) businesses#update DELETE /businesses/:id(.:format) businesses#destroy 的来电替换为business_url,但网址现在看起来像new_business_url