尽管已定义,但没有路由匹配[GET]

时间:2016-03-02 14:12:17

标签: ruby-on-rails ruby

我很难理解为什么会遇到这个问题。我查看了大多数类似的问题。在大多数情况下,作者忘记包括相关路线。但是我清楚地做了,我把这个问题归结为我的Rails知识中的空白,我希望你们中的一些人可以解决这个问题。

我不断得到的错误:No route matches [GET] "/fight.29"尽管在resources :fights中定义了它,我也尝试了其他方法,但我也没有用。

我注意到的一些事情:

  • 我的应用尝试通过http://localhost:3000/fight.29
  • 中的重定向访问http://localhost:3000/fights/29而不是FightsController
  • 当我手动输入http://localhost:3000/fights/29时,它的效果非常好。
  • 我对fights的展示操作缺少前缀
  • 在网址内的.fight之间29。不确定这意味着什么以及它是否与错误有关

以下是rake routes的输出:

            Prefix Verb   URI Pattern                                     Controller#Action
              root GET    /                                               static_pages#home
    fighter_skills GET    /fighters/:fighter_id/skills(.:format)          skills#index
                   POST   /fighters/:fighter_id/skills(.:format)          skills#create
 new_fighter_skill GET    /fighters/:fighter_id/skills/new(.:format)      skills#new
edit_fighter_skill GET    /fighters/:fighter_id/skills/:id/edit(.:format) skills#edit
     fighter_skill GET    /fighters/:fighter_id/skills/:id(.:format)      skills#show
                   PATCH  /fighters/:fighter_id/skills/:id(.:format)      skills#update
                   PUT    /fighters/:fighter_id/skills/:id(.:format)      skills#update
                   DELETE /fighters/:fighter_id/skills/:id(.:format)      skills#destroy
          fighters GET    /fighters(.:format)                             fighters#index
                   POST   /fighters(.:format)                             fighters#create
       new_fighter GET    /fighters/new(.:format)                         fighters#new
      edit_fighter GET    /fighters/:id/edit(.:format)                    fighters#edit
           fighter GET    /fighters/:id(.:format)                         fighters#show
                   PATCH  /fighters/:id(.:format)                         fighters#update
                   PUT    /fighters/:id(.:format)                         fighters#update
                   DELETE /fighters/:id(.:format)                         fighters#destroy
             fight POST   /fight(.:format)                                fights#start
         new_fight GET    /fights/new(.:format)                           fights#new
                   GET    /fights/:id(.:format)                           fights#show

这是我的routes.rb

root 'static_pages#home'

  resources :fighters do
    resources :skills
  end
  post '/fight', to: 'fights#start'
  resources :fights, only: [:new, :show]

  #get  '/fights/:id/', to: 'fights#show'
  #match '/:id' => 'fights#show', via: [:get]

这是fights_controller.rb

  def show
    @fight = Fight.find(params[:id])
  end
  def start
    @fight = Fight.create(attacker_id: params[:attacker_id], defender_id: params[:defender_id], winner: params[:winner])
    if @fight.save
      redirect_to @fight
    else
      redirect_to root_path
    end
  end

记录(另一个请求但错误相同):

Started GET "/fight.30" for 127.0.0.1 at 2016-03-02 15:10:17 +0100

ActionController::RoutingError (No route matches [GET] "/fight.30"):
  actionpack (4.2.5) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call'
  web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
  web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.5) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.5) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.5) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.5) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.4) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.5) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/static.rb:116:in `call'
  rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.5) lib/rails/engine.rb:518:in `call'
  railties (4.2.5) lib/rails/application.rb:165:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  rack (1.6.4) lib/rack/content_length.rb:15:in `call'
  rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
  /home/chrislotix/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
  /home/chrislotix/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
  /home/chrislotix/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'

我错过了什么?

编辑:

<%= link_to 'FIGHT!', fight_path(@fight, attacker_id: @attacker.id,
                                          defender_id: @defender.id,
                                          winner: compare_power_levels(@attacker, @defender)), method: :post  %>

注意:compare_power_levels在比较后只返回两个对象中的一个。

4 个答案:

答案 0 :(得分:2)

你可以尝试

redirect_to fight_path(@fight)


 <%= link_to 'FIGHT!', fight_path(@fight,{ attacker_id: @attacker.id,
                                      defender_id: @defender.id,
                                      winner: compare_power_levels(@attacker, @defender)}), method: :post  %>

https://[username]:[apikey] @api.softlayer.com/rest/v3/SoftLayer_Account/getInvoices?objectFilter={   "invoices": {     "createDate": {       "operation": "greaterThanDate",       "options": [         {           "name": "date",           "value": [             "01/22/2016"           ]         }       ]     }   } }&objectMask=mask[createDate]
Method: GET

答案 1 :(得分:1)

如果您在点击 link_to 标记时遇到此问题,请检查link_to标记中的路径。

理想情况下,它应如下所示:

<%= link_to 'show', fight_path(@fight) %>

答案 2 :(得分:1)

当rails生成带点的链接是因为您在路径中使用复数,或者任何其他&#34;集合&#34;路由。

您没有添加link_to,所以我会尝试解决:

<%= link_to 'show', fight_path(@fight) %>

Reference

更新

在您的控制器中,您需要使用正确的路径重定向:

redirect_to fight_path(@fight)

答案 3 :(得分:1)

原因在于:

post '/fight', to: 'fights#start'
resources :fights, only: [:new, :show]

post '/fight'生成命名路由fight_path,通常由resources创建。由于已经定义,resources不会覆盖它。您可以在路线中看到:

fight POST   /fight(.:format)                                fights#start

虽然它应该是:

fight GET   /fight/:id(.:format)                                fights#start

由于路线只有一个可选参数:format,因此当您致电fight_path(@user)时,@user.id将用作格式,从而产生'/fight.30'

您需要重命名post '/fight'路线:

post '/fight', to: 'fights#start', :as 'start_fight'
相关问题