2016-12-01 8 views
0

私は私のRailsブログエンジンに問題があります。全体的に、投稿の登録と作成はSQLiteデータベースで行われますが、ユーザが作成したすべての投稿を一覧表示しようとすると、ユーザのshow.html.erbビューにNoMethodErrorがスローされます:未定義のメソッド 'title'。NoMethodError in Users##未定義のメソッドtitleを#<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の初心者です。これは私の最初のプロジェクトです。すべての助けが高く評価されます。

ありがとうございます。あなたがループすなわち<%= user_logged_in.posts.title %>の内側にあるよう

答えて

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 | %>はエラーを発生させます: "定義されていないメソッド' posts 'がnilの場合:NilClass " –

+0

投稿が純粋なHTMLとして表示されていてもOKです。しかし、それは私が解決できる問題です。どうもありがとう! –

0

postspostあるべき<%= 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

問題を解決していないので、# "の未定義メソッド' post 'のエラーが発生するようになりました。おそらくここで間違っている可能性がありますか? –

+0

@DJ_Wilde編集内容を確認してください。 問題は、 '@ posts'というコレクションの' title'を呼び出そうとしていたことです。これは 'post'ではなく個々のインスタンスに対して行うべきです。したがって、 'post.title'はあなたに望ましい結果を与えます。 – dp7

関連する問題