I have a very hard time understanding why I have this issue. I looked through the majority of similar problems. In most cases authors forgot to include the relevant route. However I clearly do and I attribute this problem to gaps in my Rails knowledge which I hope some of you can address.
The error I keep getting: No route matches [GET] "/fight.29"
despite having it defined in the resources :fights
I also tried other methods which I commented out, but to no avail.
Few things I noticed:
- My app tries to access
http://localhost:3000/fight.29
instead ofhttp://localhost:3000/fights/29
through redirect in theFightsController
- When I type
http://localhost:3000/fights/29
manually it works perfectly fine. - My show action for
fights
is missing the prefix - Theres a
.
betweenfight
and29
inside url. Not sure what it means and if it has something to do with the error
Here's the output of 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
Here's my 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]
Here's the 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
Log (another request but same error):
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'
What am I missing?
EDIT:
<%= link_to 'FIGHT!', fight_path(@fight, attacker_id: @attacker.id,
defender_id: @defender.id,
winner: compare_power_levels(@attacker, @defender)), method: :post %>
Note: compare_power_levels simply returns one of the two objects after comparing.