我有两个控制台(Users
,Posts
)有两个表,但我需要从Posts
张贴的User.id
表中提取所有帖子
无论我做了什么,我都会在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'
答案 0 :(得分:3)
@post.first
此代码引发异常。您定义了@posts
而不是@post
!
答案 1 :(得分:0)
如果您想要“User.id
发布的帖子表格中的所有帖子”,请在@posts = Post.all
方法上替换@posts = @user.posts
#show