我有兩個控制器(Users
,Posts
)兩個表,但我需要把所有的職位由User.id
發佈無論我做什麼Posts
表,我收到NoMethodError
在Users#show
如何從其他控制器顯示錶格?
user_controller.rb :
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
def index
@users = User.all
@posts = Post.all
end
def show
@posts = Post.all
end
end
你是怎麼在別的控制器上顯示其他控制器的表的?
Full error:
NoMethodError in Users#show
Showing ~/users/show.html.haml where line #37 raised:
undefined method `first' for nil:NilClass
Extracted source (around line #37):
34
35 %p
36 %b TEST:
37 = @post.first
Rails.root: ~
Application Trace | Framework Trace | Full Trace
app/views/users/show.html.haml:37:in `_app_views_users_show_html_haml___489713080_88351100'
給你的代碼...這就是拋出錯誤。 –
例如,我想對此進行測試「= @ Post.first」似乎控制器用戶沒有看到Posts控制器和它的表,無論我在模型Posts中添加「belongs_to:User」,在Model中添加「has_many:posts」。順便說一句,代碼是HAMLed – user3184846
您可以添加完整的錯誤跟蹤嗎? –