Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 954189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:10:23+00:00 2026-05-16T00:10:23+00:00

I would like to create a mechanism for a User to keep track of

  • 0

I would like to create a mechanism for a User to keep track of other, favorite Users, similar to SO’s favorite questions. I’m using the Rails 3.0 beta.

To do so, I have a User-Favorite HABTM relationship, which works as expected:

class User < ActiveRecord::Base
  has_and_belongs_to_many :favorites, :class_name => "User", :join_table => "favorites", :association_foreign_key => "favorite_id", :foreign_key => "user_id"
end

The Favorites Controller only needs 3 of the 7 RESTful methods to manage a User’s favorites:

class FavoritesController < ApplicationController

  # GET /favorites
  # GET /favorites.xml
  def index

    @user = User.find(params[:user_id])
    @favorites =  @user.favorites.joins(:profile).order("last_name,first_name")

    ...

  end

  def create

    @favorite = User.find(params[:id])
    current_user.favorites << @favorite

    ...

  end

  def destroy

    @favorite = User.find(params[:id])
    current_user.favorites.delete(@favorite)

    ...

  end

end

The Routes.rb file contains the routing instruction:

resources :users, :except => :destroy do
  resources :favorites, :only => [:index,:create,:destroy]
end

that generates these user-favorite routes:

GET    /users/:user_id/favorites(.:format)            {:controller=>"favorites", :action=>"index"}
user_favorites POST   /users/:user_id/favorites(.:format)            {:controller=>"favorites", :action=>"create"}
user_favorite DELETE /users/:user_id/favorites/:id(.:format)        {:controller=>"favorites", :action=>"destroy"}

In the User’s Show View, the User (@user) can be toggled as a favorite using image links, which works as expected:

<% if [test if user is a favorite] %>

    # http://localhost:3000/favorites/destroy/:id?post=true
    <%= link_to image_tag("favorite.png", :border => 0), :controller => :favorites, :action => :destroy, :post=>true, :id => @user %>

<% else %>

    # http://localhost:3000/favorites/create/:id?post=true
    <%= link_to image_tag("not-favorite.png", :border => 0), :controller => :favorites, :action => :create, :post=>true, :id => @user %>

<% end %>

However, in the current_user’s favorite Index View, the link_to each favorite user:

# http://localhost:3010/users/4/favorites/3?post=true
<%= link_to image_tag("favorite.png", :border => 0), :controller => :favorites, :action => :destroy, :id => favorite, :post=>true %>

generates an error that reads:

No route matches “/users/4/favorites/3”

Questions:

  1. Have I correctly specified my routing? Seem like the create and destroy routes would only need the id of the favorite, as the ‘owner’ of the favorite is always current_user.
  2. If I’m simply referencing the Controller/Action in the Show view, do I even need the create/destroy routes?
  3. Why doesn’t the link_to in the Index View work correctly?
  4. Are there any improvements that can be made to the over-all approach?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:10:24+00:00Added an answer on May 16, 2026 at 12:10 am

    Your routing looks fine.

    I think there is something wrong with your link_to, though. For one thing, the RESTful way is not to specify URLs with :controller and :action parameters. The correct way is using the generated URL methods, such as user_favorite_path. Also, you need to specify the :method parameter when targeting the destroy action. This is how I think the link_to should look like:

    <%= link_to image_tag("favorite.png", :border => 0), user_favorite_path(@user, @favorite), :method => :delete %>

    I believe the reason it says no route matches that URL is because you didn’t specify the :method as :delete.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i would like to create a fixed size frame for user photos uploaded. I've
I would like to create WP sidebar widget similar to this one : As
I would like create my own collection that has all the attributes of python
I would like create a web service in ASP.Net 2.0 that will supports JSON.
Would like to create a strong password in C++. Any suggestions? I assume it
I would like to create a database backed interactive AJAX webapp which has a
I would like to create an SSL connection for generic TCP communication. I think
I would like to create a file format for my app like Quake, OO,
I would like to create a stored procedure in MySQL that took a list
I would like to create an application wide keyboard shortcut for a Java Swing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.