再会。我的问题是^我正在尝试在准备好的项目上使用 ransack。它使用差异语言和 will_paginage。目前我得到
No Ransack::Search object was provided to search_form_for!
当我尝试访问控制器的索引操作时。我应该怎么做才能使搜索工作?
控制器:
def index
if params[:set_locale]
redirect_to articles_path(locale: params[:set_locale])
else
if params[:q]
@search = Article.search(params[:q])
logger.debug '! IF 1'
@articles = @search.result(:distinct => true).
paginate page: params[:page], order: 'created_at desc',
per_page: 5 # load all matching records
else
logger.debug('! ELSE 2')
@articles = Article.paginate page: params[:page], order: 'created_at desc',
per_page: 5
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: @articles }
end
end
end
更新 1 是的,如果你对我投反对票,好吧,但你能向我解释一下吗?
更新 2 和视图
<%= search_form_for @q do |f| %>
<%= f.label :articles_title_start %>
<%= f.text_field :articles_title_start %><br />
<!-- etc... -->
<%= f.submit :value=>'Search' %>
<% end %>
更新 3 模型
class Article < ActiveRecord::Base
attr_accessible :text, :title
validates :title, :presence=>true,:uniqueness=>true
validates :text, :presence=>true
end