我是超新的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')) %>");
謝謝。我嘗試在這裏查找答案,雖然看起來很多人都有類似的錯誤,但在不同的情況下。任何建議將非常感激。
嘿,那麼簡單!謝謝! – Shane