标签云

微信群

扫码加入我们

WeChat QR Code

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 of http://localhost:3000/fights/29through redirect in the FightsControllerWhen I type http://localhost:3000/fights/29 manually it works perfectly fine.My show action for fights is missing the prefixTheres a .between fightand 29 inside url. Not sure what it means and if it has something to do with the errorHere's the output of rake routes:Prefix Verb URI Pattern Controller#Actionroot GET/ static_pages#homefighter_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#newedit_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#destroyfighters GET/fighters(.:format) fighters#index POST /fighters(.:format) fighters#create new_fighter GET/fighters/new(.:format) fighters#newedit_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#showHere's my routes.rb:root 'static_pages#home'resources :fighters doresources :skillsendpost '/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])enddef start@fight = Fight.create(attacker_id: params[:attacker_id], defender_id: params[:defender_id], winner: params[:winner])if @fight.saveredirect_to @fightelseredirect_to root_pathendendLog (another request but same error):Started GET "/fight.30" for 127.0.0.1 at 2016-03-02 15:10:17 +0100ActionController::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.


stackoverflow.com/questions/16264981/…

2019年04月22日35分06秒

You are building your link incorrectly, could you please show your code that redirects you to that error?

2019年04月22日35分06秒

BroiSatse I edited my post.

2019年04月22日35分06秒

First one gives me a connection error. Second displays same error as in my initial post.

2019年04月22日35分06秒

try first one now

2019年04月22日35分06秒

I updated my comment, the issue appears after i issue a post request and then try to redirect to the fights show page. Take a look and let me know.

2019年04月22日35分06秒

What is the need of issuing a 'post' request here ? Ideally, it should be GET request and you have also defined a routes in your routes.rb as : GET/fights/:id(.:format) fights#show

2019年04月22日35分06秒

fights_path is a path to index action, helper does not expect any params except for optional :format. This will generate link to /fights.30

2019年04月22日35分06秒

BroiSatse Yes is what I said in my answer below... That's the issue, look at his redirect_to

2019年04月22日35分06秒

SsouLlesS - you can see in his edit that he already uses fight_path, which means it is problem with fight_path url_helper defnition

2019年04月22日35分06秒

While your answer makes perfect sense and helped me understand it, the solution itself doesn't work. pitabas prathal's first solution however solved it. I believe it might have something to do with the structure of my application. The error I'm getting after your answer is 'No route matches {:action=>"show", :attacker_id=>9, :controller=>"fights", :defender_id=>8, :id=>#<Fight id: nil, created_at: nil, updated_at: nil, attacker_id: nil, defender_id: nil, winner: nil>, :winner=>8} missing required keys: [:id]'

2019年04月22日35分06秒

This is a good message, it is expected and it is a sign that the above worked as your Fight is not saved in your database - you cannot redirect to it. There are however quitea lot of things I am not very sure in your code. Firstly, never ever use links to send post requests - it is a serious security issue. Second thing - which action do you actually want to trigger with that link? :D

2019年04月22日35分06秒

That's good to hear. Whole feature of this application is very hacky right now and i believe there's a lot of room for improvement. But the rough idea is as following: I write the last two fighters inside a fight table (one labeled as attacker and other as defender, both are of fighter class) the post link is supposed to pick both fighters, determine the winner and then redirect to the show page to display the fight (the fights winner, participants etc) I based the post request on the same way i did the "Like" feature in my other project. What's the way to go? I'd love to hear your opinion.

2019年04月22日35分06秒