2016-07-04 5 views
0

RailsとjQueryの新機能で、頭の中で少し気がします。私は友人が言った攻撃的なものをログに記録してから、それを注文するためにアップ/ダウンシステムを使用する単一のページアプリを作ろうとしています。スコアがAjaxを使用して更新されたら、並べ替えのための投稿に問題があります。今、私はdownvoteアクションで実装するためにこれを取得しようとしています。関連するすべての情報をここに含めようとしましたが、もっと含める必要があるかどうかを教えてください。Ajaxを使用してNoMethodErrorを取得する(未定義のメソッド[[] 'nil:NilClass)

エラー

Completed 500 Internal Server Error in 39ms (ActiveRecord: 3.2ms) 

NoMethodError (undefined method `[]' for nil:NilClass): 
    app/views/posts/_posts.html.erb:3:in `_app_views_posts__posts_html_erb__962868812775721276_70166010949980' 
    app/views/posts/downvote.js.erb:2:in `_app_views_posts_downvote_js_erb__4067496276046646679_70165971968540' 
    app/controllers/posts_controller.rb:39:in `downvote' 


    Rendered /Users/shaneboyar/.rvm/gems/[email protected]/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_source.erb (3.0ms) 
    Rendered /Users/shaneboyar/.rvm/gems/[email protected]/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_trace.text.erb (1.5ms) 
    Rendered /Users/shaneboyar/.rvm/gems/[email protected]/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_request_and_response.text.erb (0.8ms) 
    Rendered /Users/shaneboyar/.rvm/gems/[email protected]/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/diagnostics.text.erb (47.0ms) 

投稿コントローラー

class PostsController < ApplicationController 

def index 
    @new_post = Post.new 
    @all_posts = Post.order(score: :desc).all 
end 

def create 
@new_post = Post.new(post_params) 
    if @new_post.save 
     flash[:success] = "Yeah... he probably would say that." 
     redirect_to root_path 
    end 
end 

def destroy 
    @post = Post.find(params[:id]) 
    @post.delete 
    respond_to do |format| 
    format.html { redirect_to root_path } 
    format.js 
    end 
end 

def upvote 
    @post = Post.find(params[:id]) 
    @post.score += 1 
    @post.save 
    respond_to do |format| 
    format.html { redirect_to root_path } 
    format.js 
    end 
end 

def downvote 
    @post = Post.find(params[:id]) 
    @post.score -= 1 
    @post.save 
    respond_to do |format| 
    format.html { redirect_to root_path } 
    format.json { render json: @post } 
    format.js 
    end 
end 

閲覧/投稿/ _Index.html.erb

<div class="jumbotron center"> 
    <div class="container"> 
    <h1>@MattBatt</h1> 
    <h4>Things Matt has probably said at one point.</h4> 
    </div> 
</div> 

<div class="container"> 

<!--BEGIN FORM --> 
    <%= form_for(@new_post) do |f| %> 

    <div class="form-group">    
     <div class="field"> 
     <%= f.label :'What\'d that motherfucker say now?' %><br> 
     <%= f.text_area :comment, :class => 'form-control' %> 
     </div> 

    </div> 
     <div class="actions"> 
     <%= f.button "Create", :class => 'btn btn-default btn-lg btn-block' %> 
     </div> 

    <% end %> 
<!-- END FORM --> 

<!--BEGIN ALERT --> 
    <% flash.each do |message_type, message| %> 
    <div class="alert alert-<%= message_type %>"><%= message %></div> 
<% end %> 
<!--END ALERT --> 

<!--BEGIN POSTS --> 
<%= render 'posts' %> 
<!--END POSTS --> 

</div> 

回の閲覧/投稿/ _Posts.html.erb(多分これは混乱であるように私は感じる?)

<div class="container-fluid comments" id="comments"> 
    <ul> 
    <% @all_posts[0..9].each do |p| %> 
     <li class="row"> 
     <div class="col-sm-2"> 
      <div class='post-<%=p.id%>-score score'><%= p.score %></div> 
      <div class="voters"> 
      <div class="upvoter"><%= link_to(image_tag("upArrow.png"), upvote_post_path(p), method: :patch, remote: true, :class => 'upvoter_button') %></div> 
      <div class="downvoter"><%= link_to(image_tag("downArrow.png"), downvote_post_path(p), method: :patch, remote: true, :class => 'downvoter_button') %></div> 
      </div> 
     </div> 
     <div class="comment col-sm-9"> 
      <%= p.comment %> 
      <div class="timestamp">Posted <%= time_ago_in_words(p.created_at) %> ago. 
      <% if Rails.env.development? %> 
      <%= link_to "delete", p, method: :delete, remote: true, :class => 'deleter' %> 
      <% end %> 
      </div> 
     </div> 
     </li> 
    <% end %> 
    </ul> 
</div> 

routes.rbを

Rails.application.routes.draw do 

    root 'posts#index' 

    get 'posts/index' 

    get 'posts/create' 

    resources :posts do 
    member do 
    patch 'upvote' 
    patch 'downvote' 
    end 
end 

閲覧/投稿/ downvote.js。エルブ

$(".post-<%[email protected]%>-score").html('<%= @post.score %>'); 
$("#comments").html("<%= escape_javascript(render('posts/posts')) %>"); 

ありがとうございました。私はここで答えを調べようとしましたが、多くの人が同じようなエラーに遭うようですが、状況は異なっています。どんなアドバイスも本当に感謝しています。

答えて

0

投稿をdownvote/upvoteに並べ替えるには、Railsを使っていると思いますが、効率的にJSと同じことができると思います。

コントローラのdownvoteアクションに@all_postsが設定されていないため、エラーが発生しています。

before_actionコールに追加できます。

before_action :set_all_posts, only: [:index, :downvote, :upvote] 
def set_all_posts 
    @all_posts = Post.order(score: :desc) 
end 
+0

ちょっと単純ですね!ありがとう! – Shane

関連する問題