2016-08-24 39 views
0

screenshotNoMethodError在零PostsController#給予好評未定義的方法`upvote_by」:NilClass

目前我使用的是bootstrp - 薩斯寶石一起acts_as_votable寶石,我想給予好評後點擊glyphicon圖標時,相反,它會引發我高於錯誤。 這是我posts_controller.rb文件

class PostsController < ApplicationController 
before_action :authenticate_user!, except: [:index, :show] 

    def search 
    if params[:search].present? 
     @posts = Post.search(params[:search]) 
    else 
     @posts = Post.all 
    end 
    end 

    def index 
    @posts = Post.all.order('created_at DESC')  
    end 

    def new 
    @post = Post.new  
    end 

    def create 
    @post = Post.new(post_params) 

    if @post.save 
     redirect_to @post 
    else 
     render 'new' 
    end 
    end 

    def show 
    @post = Post.find(params[:id])  
    end 

    def edit 
    @post = Post.find(params[:id])  
    end 

    def update 
    @post = Post.find(params[:id]) 

    if @post.update(params[:post].permit(:title, :date, :time, :location)) 
     redirect_to @post 
    else 
     render 'edit' 
    end 
    end 

    def destroy 
    @post = Post.find(params[:id]) 
    @post.destroy 

    redirect_to root_path  
    end 

    def upvote 
    @post.upvote_by current_user 
    redirect_to :back 
    end 

    private 
    def post_params 
    params.require(:post).permit(:title, :date, :time, :location) 
    end 
end 

這是我show.html.erb文件

<div id="post_content"> 
<h1 class="title"> 
    <%= @post.title %> 
</h1> 

<p class="date"> 
    Submitted <%= time_ago_in_words(@post.created_at) %> Ago 
    <% if user_signed_in? %> 
    | <%= link_to 'Edit', edit_post_path(@post) %> 
    | <%= link_to 'Delete', post_path(@post), method: :delete, data: { confirm: 'Are you sure?' } %> 
    <% end %> 
</p> 

<p class="date"> 
    <%= @post.date %> 
</p> 

<p class="time"> 
    <%= @post.time %> 
</p> 

<p class="location"> 
    <%= @post.location %> 
</p> 

<div id="comments"> 

    <div class="btn-group pull-left"> 
    <%= link_to like_post_path(@post), method: :put, class: "btn btn-default" do %> 
     <span class="glyphicon glyphicon-heart"></span> 
     <%= @post.get_upvotes.size %> 
    <% end %> 
    </div> 

    <h2><%= @post.comments.count %> Comments</h2> 
    <%= render @post.comments %> 

    <h3>Add a comment:</h3> 
    <%= render "comments/form" %> 
</div> 

這是我的routes.rb文件

Rails.application.routes.draw do 

    mount RailsAdmin::Engine => '/admin', as: 'rails_admin' 
    devise_for :users 
     resources :posts do 
     resources :comments 
     collection do 
     get 'search' 
     end 

     member do 
      put "like", to: "posts#upvote" 
     end 
    end 

    root "posts#index" 

    get '/about', to: 'pages#about' 
end 

這是我的崗位模型

class Post < ActiveRecord::Base 

    acts_as_votable 
    searchkick 
    has_many :comments, dependent: :destroy 
    validates :title, presence: true, length: {minimum: 5} 
    validates :date, :time, :location, presence: true 
    belongs_to :user 
end 
+0

在您的控制器給予好評方法@post是零,這就是爲什麼你得到這個錯誤。 – Navin

+0

感謝大家的快速回復,非常感謝您的幫助。 – Bikal

回答

0

NoMethodError在PostsController#給予好評未定義的方法`upvote_by」 爲無:NilClass

的錯誤是因爲您沒有在中初始化方法,以便@post

def upvote 
    @post = Post.find(params[:id]) 
    @post.upvote_by current_user 
    redirect_to :back 
end 
相關問題