2016-12-01 36 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

相關問題