0


Hey there,
I am fairly new to Rails and I've managed to create a Favorite controller for my Items(Tools) and Users, which works totally fine.
Now I am trying to do the same just in that user module. So users are able to favorite other users.

I played around, and came up with this:

I am getting this error in the browser when accessing /users/index view:

NoMethodError in Users#index undefined method `favorite_user_path' for #<#<Class:0x8ca77b8>:0x8ca50b8> 

Here is my code:

app/models/favorite_user.rb

class FavoriteUser < ActiveRecord::Base belongs_to :c_user_id belongs_to :user_id end 

app/models/user.rb

class User < ActiveRecord::Base # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable has_many :tools # Favorite tools of user has_many :favorite_tools # just the 'relationships' has_many :favorites, through: :favorite_tools, source: :tool # the actual tools the user favorites # Favorite users of user has_many :favorite_users # just the 'relationships' has_many :userfavorites, through: :favorite_users, source: :user # the actual users the user favorites has_many :userfavorited_by, through: :favourite_users, source: :user # the actual users favoriting a user mount_uploader :avatar_filename, AvatarUploader end 

app/controllers/users_controller.rb

class UsersController < ApplicationController before_action :find_user, only: [:show, :favorite] # Add and remove favorite recipes # for current_user def userfavorite type = params[:type] if type == "favorite" current_user.userfavorites << @user elsif type == "unfavorite" current_user.userfavorites.delete(@user) else # Type missing, nothing happens redirect_to :back, notice: 'Nothing happened.' end end def index @users = User.all end def show @tools = Tool.where(user_id: @user).order("created_at DESC") @tool = Tool.find(1) end private def find_user @user = User.find(params[:id]) end end 

app/views/users/index.html.haml

- @users.each do |user| = image_tag gravatar_for user if user.use_gravatar == true = image_tag user.avatar_filename.url if user.use_gravatar == false %h2= link_to user.username, user %p= link_to "Favorite", favorite_user_path(user, type: "favorite"), method: :get %p= link_to "Unfavorite", favorite_user_path(user, type: "unfavorite"), method: :get 

app/config/routes.rb

resources :users, only: [:index, :show, :userfavorite] do get :userfavorite, on: :member end 

I hope the provided data is enough, if not please tell me. I'm grateful for all Your replies.

2
  • Your route is userfavorite. So that'd be userfavorite_user_path. Commented Sep 5, 2016 at 16:21
  • @Ven That works, don't know how I missed that - never copy code from what you've done already and then change something I guess! A question though on the user model: Is it generally possible what I am doing there? Just because I am linking to a model which does not exist, but instead I would like to link to the exact same model I am linking from. Can I just delete this piece of code: 'source: :user'? Commented Sep 5, 2016 at 16:32

1 Answer 1

1

You haven't declared any such path in the routes file. As per your routes file you can use named path like

userfavorite_user_path(user_id) 
Sign up to request clarification or add additional context in comments.

3 Comments

That works, don't know how I missed that - never copy code from what you've done already and then change something I guess! A question though on the user model: Is it generally possible what I am doing there?
Just because I am linking to a model which does not exist, but instead I would like to link to the exact same model I am linking from. Can I just delete this piece of code: 'source: :user'?
There's a close reason on SO that's "simple typographical error". Answering such questions to get some rep is... well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.