2016-07-04 91 views
0

我是超新的Rails和jQuery,感觉有点在我头上。我只是想制作一个单一页面的应用程序,让我记录我的朋友说过的冒犯性内容,然后使用up and downvote系统来订购它们。使用Ajax更新分数后,我无法重新排序。现在我只是试图让这个实施downvote行动。我试图在这里包含所有相关信息,但是让我知道是否需要包含更多信息。使用Ajax和获取NoMethodError(未定义的方法'[]'为零: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。 erb

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

谢谢。我尝试在这里查找答案,虽然看起来很多人都有类似的错误,但在不同的情况下。任何建议将非常感激。

回答

0

如果你想重新订购downvote/upvote上的帖子,我假设你会使用Rails来做到这一点,尽管我认为你可以在JS上有效地做到这一点。

您收到错误,因为@all_posts未在您的控制器的downvote操作中设置。

您可以将其添加到before_action调用。

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

嘿,那么简单!谢谢! – Shane

相关问题