So after pulling my hair out for an hour, I decided to post this here. I get this error:
NoMethodError in Articles#show
Showing /home/leon/sites/VIS/app/views/articles/_article.html.erb where line #3 raised:
undefined method `user_path' for #<#<Class:0x7fdd81fe1eb8>:0x7fdd81fdfdc0>
Extracted source (around line #3):
1: <div id="left-column">
2: <p class="label-b">Author<br>
3: <%= link_to "#{@article.user.penname}'s profile", user_path(article.user) %>
user_path(article.user) %>
When I try to link to the article’s authoring user. My routes file:
resources :tags
get "admin/index"
get "admin/show"
get "articles/contact"
get "articles/about"
resources :roles
devise_for :users, :controllers => { :registrations => "users/registrations" }
resources :articles do
resources :comments
end
I do have a belongs_to :user in my article model file and a has_many :articles in my user model file. I’m using this link to link to the author’s profile within the article.
Please help! I’m using can can for my permission management and devise for my authentication, but its throwing the error within the show action of the article so I didn’t post that code. Let me know if I should. Thanks!
@Jacob
I get the same error:
NoMethodError in Articles#show
Showing /home/leon/sites/VIS/app/views/articles/_article.html.erb where line #3 raised:
undefined method `user_path’ for #<#:0x7fdd82217a98>
Extracted source (around line #3):
1:
2: Author
3: <%= link_to “#{@article.user.penname}’s profile”, article.user %>
4: Previous Versions
5: Version 7
6: <%= image_tag “add-to-favorites.png”, :class => “favorites-button” %>
The relevant part of my routes:
{:action=>"destroy", :controller=>"roles"}
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session GET /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_password POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
user_password PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
user_registration POST /users(.:format) {:action=>"create", :controller=>"users/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"users/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"users/registrations"}
user_registration PUT /users(.:format) {:action=>"update", :controller=>"users/registrations"}
user_registration DELETE /users(.:format) {:action=>"destroy", :controller=>"users/registrations"}
user_confirmation POST /users/confirmation(.:format) {:action=>"create", :controller=>"devise/confirmations"}
new_user_confirmation GET /users/confirmation/new(.:format) {:action=>"new", :controller=>"devise/confirmations"}
user_confirmation GET /users/confirmation(.:format) {:action=>"show", :controller=>"devise/confirmations"}
Aha! It doesn’t have a route for user#show, but i’m not sure why as I have:
class UsersController < ApplicationController
...
def show
respond_to do |format|
format.json { render :json => @user }
format.xml { render :xml => @user }
format.html
end
rescue ActiveRecord::RecordNotFound
respond_to_not_found(:json, :xml, :html)
end
in my app/controllers folder. Do I need to move my users controller into app/controllers/devise and overload it with devise instead of application controller?
I figured it out. After playing around with the routes for a while and reading the rails documentation, I added this line to my routes file:
devise_for :users, :controllers => { :registrations => “users/registrations” }
resources :users, :only => [:index, :show] <== THIS LINE
below my devise_for routes. This allows me to display the user profiles without having to worry about the edit update etc. roles that devise provides for. Thanks for the answers anyway guys, I appreciate it!