2016-12-01 55 views
0

我的Rails博客引擎有问题。总体而言,注册和创建帖子作品(它们在SQLite数据库中形成),但是当我尝试列出用户创建的所有帖子时,它会在用户的show.html.erb视图中引发一个NoMethodError:未定义的方法“标题”。NoMethodError在用户#show,未定义的方法标题为#<Post :: ActiveRecord_Associations_CollectionProxy:0x00000004c5e5f0>

PostsController.rb

class PostsController < ApplicationController 
    before_action :user_is_required, only: [:create, :destroy] 

    def index 
    @posts = Post.all 
    end 

    def new 
    @post = Post.new 
    end 

    def create 
    @post = user_logged_in.posts.create(post_params) 
    if @post.save 
    flash[:success] = "Your post has been successfully saved." 
     redirect_to root_path 
    else 
     flash[:danger] = "Oops! Something went wrong. Try again." 
     redirect_to new_post_path 
    end 
    end 

    def edit 
    end 

    def update 
    if @post.update_attributes(post_params) 
     flash[:success] = "Your post has been successfully updated." 
     redirect_to post_path(@posts) 
    else 
     flash[:danger] = "Oops! Something went wrong. Try again." 
     render 'post' 
    end 
    end 

    def show 
    @post = Post.find(params[:id]) 
    end 

    def destroy 
    if @post.destroy 
     flash[:success] = "Your post has been successfully deleted." 
     redirect_to posts_path 
    else 
     flash[:danger] = "Oops! Something went wrong. Try again." 
    end 
    end 

    private 

    def post_params 
    params.require(:post).permit(:title, :content) 
    end 

end 

show.html.erb(用户视图)

<div class="posts"> 
    <div class="container marketing"> 
    <div class="col-lg-4 col-md-4 col-xs-12 col-sm-12"> 
     <section id="info"> 
      <%= image_tag("#", :class => "user_avatar") %> 
      <h2 class="user_name"> 
       <%= user_logged_in.name %> 
      </h2> 
     </section> 
    <% if user_logged_in %> 
    <%= link_to "Change avatar", class: "btn btn-primary" %><br> 
    <% end %> 

    <%= link_to "Follow", class: "btn btn-primary" %> 
    </div> 
    <div class="col-lg-8 col-md-8 col-sm-12 col-xs-12"> 
     <h2> 
      <%= user_logged_in.name %> has <%= user_logged_in.posts.count %> posts. 
     </h2> 
     <% if user_logged_in.posts.any? %> 
      <% @posts.each do |post| %> 
      <h2 class="title"> 
       <%= user_logged_in.posts.title %> # The line which raises the error 
      </h2> 
      <p class="post"> 
       <%= user_logged_in.posts.content %> 
      </p> 
      <% end %> 
     <% end %> 

    </div> 
</div> 

post.rb

class Post < ActiveRecord::Base 
    belongs_to :user 
    default_scope -> { order(created_at: :desc) } 
    validates :user_id, presence: true 
    validates :title, presence: true 
    validates :content, presence: true 

end 

我是Ruby on Rails的新手,这是我的第一个项目。任何和所有的帮助将不胜感激。

在此先感谢。

回答

0

想想逻辑有点混乱这里。

user_logged_in.posts是一个关系,所以它返回与登录用户关联的帖子。 @posts设置为表中的所有帖子。

<% if user_logged_in.posts.any? %> 
    <% @posts.each do |post| %> 
    <h2 class="title"> 
     <%= user_logged_in.posts.title %> # The line which raises the error 
    </h2> 
    <p class="post"> 
     <%= user_logged_in.posts.content %> 
    </p> 
    <% end %> 
<% end %> 

这是说,如果登录的用户拥有任何职位,遍历所有职位(不只是用户的帖子),然后我想你想显示的用户的帖子,但你会语法错误。试着改变你的代码如下:

​​

这将遍历用户的帖子,分配每个岗位的post变量,然后显示标题和帖子的内容。

+0

我尝试了您提供的代码片段,但仍然无法使用:<%@ user_logged_in.posts.each do | post | %>引发错误:“未定义的方法'帖子为零:NilClass” –

+0

好吧,现在它可以工作,尽管帖子显示为纯HTML。但这是我可以解决的问题。非常感谢! –

0

postspost因为你是内循环即<%= user_logged_in.posts.title %>应该<%= post.title %>

<% @posts.each do |post| %> 
    <h2 class="title"> 
    <%= post.title %> # The line which raises the error 
    </h2> 
    <p class="post"> 
    <%= post.content %> 
    </p> 
<% end %> 
+0

没有解决问题,现在它引发了一个错误“未定义的方法'后'为#<用户:0x000000054cfc98>”。这里可能会出现什么问题? –

+0

@DJ_Wilde请检查编辑。 问题是你正试图为集合'@ posts'调用'title',你应该为单个实例,而不是'post'完成。所以,'post.title'会给你想要的结果。 – dp7

相关问题