ruby on rails - Rails3 nested-routing issues -


I want to create a mechanism to track the other favorite user for a user, just like the questions of the SO I am using Rail 3.0 beta.

To do this, I have a user-favorite HABTM connection, which works as expected:

  class user & lt; ActiveRecord :: Base has_and_belongs_to_many: Favorites,: class_name = & gt; "User",: join_table = & gt; "Favorites",: association_foreign_key = & gt; "Favorite_id" ,: foreign_key => "User_ID" and  

The Favorites Controller requires only 3 of 7 ways in order to manage the user's favorites:

  Category Preferred Controller & Lt; ApplicationController # GET / Favorite #GET / lovesites.xml DEF Index @ User = User.Fid (Param [[User_ID]) @ Patience = User Piesz.zx (profiles) .dr ("last_name, first_name") ... Make an end DEF @ favor = user.fund (PARAM [: ID]) current_UUG Favorites & lt; & Lt; @textem ... destroy the end df @testim = userfund (param [: id]) current_user.favorites.delete (@custom) ... and end  

routes. RB files have routing instructions:

  resources: user, except => : Delete resources: favorites, only = & gt; [: Index, Create], Delete  

which generates these user-preferred routes:

  received / user /: user_id / favorites ( .: Format) {: Controller = & gt; "Favorite",: Action => "Index"} User's preferred post / user / user / addtion (.: Format) {: controller = & gt; "Favorite",: Action => "Create"} user_favorite DELETE /users/:user_id/favorites/:id(.format) {: Controller => "Favorite",: verb = & gt; "Destroy"}  

In the user's show view, the user (user) can be toggled as a favorite using the image links, which works as expected:

  & lt;% if [test is user preferred]% & Gt; # Http: // Local Host: 3000 / Favorites / Delete /: id? Post = true & lt;% = link_to image_tag ("favorite.png", border => 0),: controller => : Favorite, Action => : Delete, post = & gt; True ,: ID = & gt; @ User% & gt; & Lt;% else% & gt; #http: // localhost: 3000 / favorites / create /: id? Post = true & lt;% = link_to image_tag ("not-favorite.png", border = & gt;) ,: Controller = & gt; : Favorite, Action => : Create, post = & gt; True,: ID = & gt; @ User% & gt; & Lt;% end% & gt; However, in the current user's favorite index view, link to each preferred user:  
  # http: // localhost: 3010 / user / 4 / Favorite / 3? Post = true & lt;% = link_to image_tag ("favorite.png", border = & gt;) ,: controller => : Favorite, Action => : Destroyed, ID = & gt; Favorite, post = & gt; Correct% & gt;  

Generates an error that reads:

No routes match "/ user / 4 / favorite / 3"

:

  1. Am I properly designing my routing? It seems that the way to create and destroy will only require the ID of the favorite, because the 'owner' of the Favorites is always on.
  2. If I only refer to the controller / action in the scene view, do I also need to create / delete routes?
  3. Why does not the link work properly in the index view?
  4. Is there any improvement that is greater than

Your routing Looks fine

I think there is something wrong in your link_to, though. For one thing, to specify the URL without having to specify the way: Controller and: Action parameters are correct using the generated URL methods, such as user_favorite_path In addition, you need to specify: method parameter when The goal is to destroy the goal. It seems as if I think link_to should look like this:

<% = link_to image_tag ("favorite.png", border => 0), user_favorite_path (@user) , @extite),: Method =>: Delete%>

I believe the reason for this does not tell why the URL is not because you have not specified: method : Delete.


Comments