Spree :: Admin ::评论#index中的NoMethodError

时间:2017-11-09 08:07:39

标签: ruby-on-rails ruby ruby-on-rails-4 spree ransack

我是新来的狂欢和ruby on rails.While点击'评论'在狂欢后端,它会抛出一个错误。

先谢谢。

Spree :: Admin ::评论#index

中的NoMethodError

显示 - var / lib / gems / 2.3.0 / ruby​​ / 2.3.0 / bundler / gems / spree_reviews-e51cae9627f9 / app / views / spree / admin / reviews / index.html.erb其中第25行引发:

enter image description here

index.html.erb:

<% content_for :page_title do %>
<%= Spree.t('reviews') %>
<% end %>
<% render 'spree/admin/shared/product_sub_menu' %>
<% content_for :table_filter_title do %>
<%= Spree.t('search') %>
<% end %>
<% content_for :table_filter do %>
<div data-hook="admin_reviews_index_search">

<%= search_form_for [:admin, @search] do |f| %>
    <div class="alpha three columns">
        <div class="field">
            <%= label_tag nil, Spree.t(:user) %><br />
            <%= f.text_field :name_cont, :size => 25 %>
        </div>
    </div>

    <div class="seven columns">
        <div class="field">
            <%= label_tag nil, Spree.t(:title) -%><br/>
            <%= f.text_field :title_cont, :size => 25  -%>
        </div>
    </div>

    <div class="four columns">
        <div class="field">
            <%= label_tag nil, Spree.t(:review) -%><br/>
            <%= f.text_field :review_cont, :size => 25  -%>
        </div>
    </div>

    <div class="two columns omega">
        <div class="field">
            <%= label_tag nil, Spree.t(:approval_status)-%><br/>
            <%= f.select :approved_eq, [[Spree.t('all'), nil], 
   [Spree.t('approved_reviews'), true], 
   [Spree.t('unapproved_reviews'),false]]  -%>
     </div>
     </div>

     <div class="clear"></div>

     <div class="form-buttons actions filter-actions" data-
      hook="admin_reviews_index_search_buttons">
        <%= button Spree.t(:search), 'icon-search' %>
      </div>
      <%- end -%>
  </div>
  <%- end -%>

<% paginate @reviews %>

<% if @reviews.any? %>
<table class="index">
    <colgroup>
        <col style="width: 25%;">
        <col style="width: 10%;">
        <col style="width: 10%;">
        <col style="width: 20%;">
        <col style="width: 15%;">
        <col style="width: 17%;">
    </colgroup>
    <thead>
        <tr>
            <th><%= Spree.t('product') %></th>
            <th><%= Spree::Review.human_attribute_name(:rating) %></th>
            <th><%= Spree.t('feedback') %></th>
            <th><%= Spree::Review.human_attribute_name(:user) %></th>
            <th><%= Spree::Review.human_attribute_name(:created_at) %></th>
        </tr>
    </thead>
    <tbody>
    <%- @reviews.each do |review| -%>
        <tr id="<%= dom_id review %>">
            <td>
                <% if review.product %>
                    <%= link_to review.product.name, product_path(review.product) %>
                <% end %>
            </td>
            <td class="align-center">
                <%= txt_stars(review.rating) %>
            </td>
            <td class="align-center">
                <%= link_to "(#{review.feedback_stars}/#{review.feedback_reviews.size})", admin_review_feedback_reviews_path(review) %>
            </td>
            <td class="align-center">
                <%= review.user_id ? link_to(review.user.try(:email), [:admin, review.user]) : Spree.t(:anonymous) %></p>
                <p><%= Spree::Review.human_attribute_name(:ip_address) %>: <%= review.ip_address ? link_to(review.ip_address, "http://whois.domaintools.com/#{review.ip_address}") : '-' %></p>
            </td>
            <td class="align-center">
                <%= l review.created_at, :format => :short %>
            </td>
            <td class="actions">
                <%= link_to_with_icon 'check', Spree.t('approve'), approve_admin_review_url(review), :no_text => true, class: 'approve' unless review.approved %>
                &nbsp;
                <%= link_to_edit review, :no_text => true, :class => 'edit' %>
                &nbsp;
                <%= link_to_delete review, :no_text => true %>
            </td>
        </tr>
    <% end %>
    </tbody>
</table>
<% else %>
<div class="no-objects-found">
    <%= Spree.t(:no_results) %>
</div>
<% end %>

<%= paginate @reviews -%>

reviews_controller

class Spree::Admin::ReviewsController < 
 Spree::Admin::ResourceController
 helper Spree::ReviewsHelper
 def index
   @reviews = collection
   params[:q] ||= {}
   @search = Spree::Review.ransack(params[:q])
   @collection = @search.result.includes([:product, :user, 
   :feedback_reviews]).page(params[:page]).per(params[:per_page])
 end
 def approve
 r = Spree::Review.find(params[:id])

 if r.update_attribute(:approved, true)
   flash[:notice] = Spree.t("info_approve_review")
 else
   flash[:error] = Spree.t("error_approve_review")
end
  redirect_to admin_reviews_path
end
def edit
if @review.product.nil?
  flash[:error] = Spree.t("error_no_product")
  redirect_to admin_reviews_path and return
end
end
 private
   def collection
   params[:q] ||= {}
   @search = Spree::Review.ransack(params[:q])
   @collection = @search.result.includes([:product, :user, 
  :feedback_reviews]).page(params[:page]).per(params[:per_page])
  end
 end

review.rb

class Spree::Review < ActiveRecord::Base

      belongs_to :product, touch: true
      belongs_to :user, :class_name => Spree.user_class.to_s
      has_many   :feedback_reviews

      after_save :recalculate_product_rating, :if => :approved?
      after_destroy :recalculate_product_rating

      validates :name, presence: true
      validates :review, presence: true

      validates :rating, numericality: { only_integer: true,
      greater_than_or_equal_to: 1, 
      less_than_or_equal_to: 5,
      message: Spree.t('you_must_enter_value_for_rating') }


      default_scope { order("spree_reviews.created_at DESC") }

      scope :localized, ->(lc) { where('spree_reviews.locale = ?', lc) }
      scope :most_recent_first, -> { order('spree_reviews.created_at DESC') }
      scope :oldest_first, -> { reorder('spree_reviews.created_at ASC') }
      scope :preview, -> { limit(Spree::Reviews::Config[:preview_size]).oldest_first }
      scope :approved, -> { where(approved: true) }
      scope :not_approved, -> { where(approved: false) }
      scope :default_approval_filter, -> { Spree::Reviews::Config[:include_unapproved_reviews] ? all : approved }

      def feedback_stars
      return 0 if feedback_reviews.size <= 0
      ((feedback_reviews.sum(:rating) / feedback_reviews.size) + 0.5).floor
      end

      def set_search
      @search=Product.search(params[:q])
      end

      def recalculate_product_rating
      self.product.recalculate_rating if product.present?
      end
      end

1 个答案:

答案 0 :(得分:0)

尝试在搜索后添加结果方法:

@search = Spree::Review.ransack(params[:q]).result