<variable>_cont 的未定义方法 - Rails 上的 Ruby
Undefined method for <variable>_cont in spree - Ruby on Rails
我正在尝试在评论列表页面的 spree 后端进行搜索。
但是我得到了字段的 未定义方法错误。例如:未定义的方法 title_cont' , undefined method
review _cont'。
特此发布我的控制器:
reviews_controller.rb
class Spree::Admin::ReviewsController < Spree::Admin::ResourceController
helper Spree::ReviewsHelper
def index
@reviews = Spree::Review.ransack(params[:q]).result
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
Index.html.erb
<% 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="four 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 ,:value => ""%>
</div>
</div>
<div class="two columns omega">
</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 -%>
当我使用上述变量时变得未定义。请帮我解决这个问题。
@cliffs - 为了能够使用 ransack gem 进行搜索,您首先必须将要执行搜索的所有字段列入白名单。因此,如果您从 Spree::Base
-
继承模型,请在您的 review.rb
模型中添加以下行
self.whitelisted_ransackable_attributes = %w[title, review]
否则,如果您从 ActiveRecord::Base
继承它,则必须将 ransackable_attributes
定义为 class 方法
def self.ransackable_attributes(auth_object = nil)
['title','review','user']
end
现在尝试使用 ransack
进行搜索。
希望对您有所帮助。
问题已解决!
在关联模型 Review.rb
中为该模型的自定义可搜索属性定义了 ransackable_attributes 方法。
def self.ransackable_attributes(auth_object = nil)
['title','review','user']
end
我正在尝试在评论列表页面的 spree 后端进行搜索。
但是我得到了字段的 未定义方法错误。例如:未定义的方法 title_cont' , undefined method
review _cont'。
特此发布我的控制器:
reviews_controller.rb
class Spree::Admin::ReviewsController < Spree::Admin::ResourceController
helper Spree::ReviewsHelper
def index
@reviews = Spree::Review.ransack(params[:q]).result
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
Index.html.erb
<% 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="four 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 ,:value => ""%>
</div>
</div>
<div class="two columns omega">
</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 -%>
当我使用上述变量时变得未定义。请帮我解决这个问题。
@cliffs - 为了能够使用 ransack gem 进行搜索,您首先必须将要执行搜索的所有字段列入白名单。因此,如果您从 Spree::Base
-
review.rb
模型中添加以下行
self.whitelisted_ransackable_attributes = %w[title, review]
否则,如果您从 ActiveRecord::Base
继承它,则必须将 ransackable_attributes
定义为 class 方法
def self.ransackable_attributes(auth_object = nil)
['title','review','user']
end
现在尝试使用 ransack
进行搜索。
希望对您有所帮助。
问题已解决!
在关联模型 Review.rb
中为该模型的自定义可搜索属性定义了 ransackable_attributes 方法。
def self.ransackable_attributes(auth_object = nil)
['title','review','user']
end