それで、1時間髪を抜いた後、これをここに投稿することにしました. 次のエラーが表示されます。
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) %>
記事の作成ユーザーにリンクしようとすると。私のルートファイル:
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
記事モデル ファイルに belongs_to :user があり、ユーザー モデル ファイルに has_many :articles があります。このリンクを使用して、記事内の著者のプロフィールにリンクしています。
助けてください!私は権限管理にcan canを使用し、認証にdeviseを使用していますが、記事のshowアクション内でエラーがスローされるため、そのコードは投稿しませんでした。すべきかどうか教えてください。ありがとう!
@ジェイコブ
同じエラーが表示されます:
記事の NoMethodError#show
/home/leon/sites/VIS/app/views/articles/_article.html.erb を表示して、3 行目が発生しました:
undefined method `user_path' for #<#:0x7fdd82217a98> 抽出されたソース (行 #3 付近):
1: 2: 作成者
3: <%= link_to "#{@article.user.penname}'s profile", article.user %> 4: 以前のバージョン 5: バージョン 7
私のルートの関連部分:
{: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"}
あはは!user#show のルートはありませんが、理由がわかりません。
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
app/controllers フォルダーにあります。ユーザーコントローラーを app/controllers/devise に移動し、アプリケーションコントローラーの代わりにデバイスでオーバーロードする必要がありますか?