임 레일의 연결에 문제가 :레일
은 현재 내가 포스트 및 사용자 모델을 가지고 있고, 관계가 이런 식으로 설정됩니다 내 애플 리케이션에,
class User < ActiveRecord::Base
attr_accessible :username, :name, :lastname
has_many :posts
end
class Post < ActiveRecord::Base
attr_accessible :title, :body
belongs_to :user
end
그러나
: 이것은 내이다undefined method `name' for nil:NilClass
: 임 내가이 오류가 게시물에 대한 사용자 이름에 액세스하려고 할 때 /views/posts/index.html.haml posts
테이블 user_id
라는 이름의 열이있는 경우 임 잘못 appretiated 될 것입니다 무엇을의 6,
- title "Posts"
%table
%tr
%th Title
%th Body
%th Author
- for post in @posts
%tr
%td= h post.title
%td= h post.body
%td= h post.user.name
%td= link_to 'Show', post
%td= link_to 'Edit', edit_post_path(post)
%td= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete
%p= link_to "New Post", new_post_path
어떤 생각이 모두의
나는 행동 부분에있는 코드를 보여줄 것이다. –
사용자 집합이없는'@ posts'에 하나 이상의 (또는 그 이상)'Post' 모델이있는 것처럼 보입니다. –
@piemersons 컨트롤러? def show @ post = find (params [: id]) end –