我有一个link_to,可以将用户带到他们的个人资料页面。以前它曾经工作但现在我继续获得一个未初始化的常量UsersController。
路线:
Rails.application.routes.draw do
resources :users
resources :questions
devise_for :users
root 'home#index'
get '/users/:id' => 'home#profile'
end
Index.html.erb:
<div class="row">
<div class="col-md-8">
<% if @questions.any? %>
<% @questions.each do |question| %>
<div class="well">
<div class="media">
<a class="pull-left">
<% if question.user.avatar.blank? %>
<img src="http://www.adtechnology.co.uk/images/UGM-default-user.png" style="width: 75px;">
<% else %>
<%= image_tag question.user.avatar, :style => "width:75px;" %>
<% end %>
</a>
<div class="media-body">
<h4 class="media-heading"><%= link_to question.title, question_path(question), :class => "ques" %></h4>
<p class="text-right">By <%= link_to question.user.username, question.user, :class => " bg" %></p> <!-- that's what's causing the error -->
<p class="text-muted"><%= truncate(question.description, :length => 50) %></p>
<ul class="list-inline navbar-right list-unstyled">
<li><span style="padding-right: 10px;" ><i class="fa fa-calendar"></i> asked <%= time_ago_in_words(question.created_at) %> ago </span></li>
</ul>
</div>
</div>
</div>
<% end %>
<% else %>
<p>No content yet</p>
<% end %>
耙路线:
users_path GET /users(.:format) users#index
POST /users(.:format) users#create
new_user_path GET /users/new(.:format) users#new
edit_user_path GET /users/:id/edit(.:format) users#edit
user_path GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
questions_path GET /questions(.:format) questions#index
POST /questions(.:format) questions#create
new_question_path GET /questions/new(.:format) questions#new
edit_question_path GET /questions/:id/edit(.:format) questions#edit
question_path GET /questions/:id(.:format) questions#show
PATCH /questions/:id(.:format) questions#update
PUT /questions/:id(.:format) questions#update
DELETE /questions/:id(.:format) questions#destroy
new_user_session_path GET /users/sign_in(.:format) devise/sessions#new
user_session_path POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session_path DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password_path POST /users/password(.:format) devise/passwords#create
new_user_password_path GET /users/password/new(.:format) devise/passwords#new
edit_user_password_path GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration_path GET /users/cancel(.:format) devise/registrations#cancel
user_registration_path POST /users(.:format) devise/registrations#create
new_user_registration_path GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration_path GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root_path GET / home#index
GET /users/:id(.:format) home#profile
正如我之前所说,我真的不知道导致错误的原因。它昨天工作正常,我根本没有做任何改变。
答案 0 :(得分:0)
您应该使用<%= link_to home_profile(question.user.id) %>
。
假设 question.user.id 获取用户的相应ID。
使用params [:id]在Profile控制器中访问用户的ID。
答案 1 :(得分:0)
我通过将资源放在底部来修复它:
Rails.application.routes.draw do
devise_for :users
root 'home#index'
get '/users/:id' => 'home#profile'
resources :users
resources :questions
end
我的猜测是它覆盖了devise_for :users