Wednesday, 15 February 2012

Ruby on Rails choosing wrong controller action -


Today I was transported to some strange (and very inconvenient) rails on rail behavior, with the Net continuously facing having had. Satisfactory Answer Note: I have translated method and root names for reading in English, and hopefully I have not presented any discrepancies.

Status

Environment

Runs on Rails 4.2.0 under Ruby Ruby 2.0 (also tested under Ruby 2.2.0)

Relevant Code

Consider the controller among others with these controllers:

  class assignment controller & lt; ApplicationController def update ... end def takeover_confirmation ... end end  

routes.rb

Because I use manually defined routes, I use resources in the not routes. RB The routes in question are defined as follows:

  ... post 'assignments /: id' = & gt; 'Assignment # Update', such as: 'Assignment' post 'assignment / takeover_structure' = & gt;  

Relevant output of rake routes :

  assignment post / assignment /: id ( : Format) Assignment # Update Assignment_takeover_confirmation POST / Assignments / Tokenover_confirmation (. Format) Assignment # TechOver_confirmation  

Problem

When I post to Assignment_takeover_confirmation_path , instead the route route to the update method. Server log:

  "/ assignment / takeover_confirmation" to start: :: 1 at ... process processed by controller # update as HTML  
< H2> Mitigation

If I have inserted update after the root definition to takeover_confirmation , then as a purpose Works (no posting to post any updates though).

Besides, after writing all this, I came to know that I used the wrong request type for the "code> update method. Do it in routes) instead of patch. RB actually resolves my problem:

  Patch 'assignment /: id' = & gt; 'Assignment # Update', such as: 'Assignment'  

However, while defining it as a post, the rail should request any POST request from the current path "/ assignment / takeover_confirmation" A completely different action, should it? I am afraid that next time I use two POST routes for the same controller, it will do the same thing.

It seems that I have a serious misunderstanding of the railway route, but I can not keep my finger on it ...

Edit: Solution

As Kotfarm explained that the above request route assignment /: id for / assignments / takeover_confirmation as the rail defined "takeover_confirmation" part as string and used for it : Id parameter Thus, it is expected to behave completely.

Functionality Example

For the sake of completeness, here is a work (if minimal) roadmap, as it should, comment by Chris's inspiration:

  Resource: Assignments are posted 'Takeover_confirmation' and end  

In this example, only the manual manually constructed route is clearly defined. The route for shows, shows, etc. (which I first manually Now defined by resources: assignment

excerpts related to rake routes :

  ... takeover_confirmation_assignments POST / Assistant / Tokenover_confirmation (.formatting) Assignment # TechOver_synthesis ... Assignment Received / Assistant / Advertising (. Format) Assignment # ShowPATCH /assignments/:id(.:format) Assignment # Update PUT / Handle /:id(.:format) Assignment # Update DELETE /assignments/:id(.:format) Assignment # deleted .... Thanks for the help!  

However, when defining this, / Assignment / Takeover_confirmation "should not be directed to take completely different actions, what should it be?

Rail routing matches the exact same order, which is defined in the routes.rb file (from top to bottom). So if it matches a certain rule (and / assignments / takeover_confirmation matches assignments /: id rule), it stops the routing process.

This behavior is simple and efficient I think any kind of "smart" matching will be the result of cumbersome and unexpected results.

It is BTW that all the routes caught are defined very below the routing file.


No comments:

Post a Comment