为什么不:q params传递给控制器?!
这是控制器代码:
def advanced_search
@search = Isbn.where(:client_id => current_user.client_id).search(params[:q])
@search.build_grouping unless @search.groupings.any?
@isbns = params[:distinct].to_i.zero? ? @search.result : @search.result(distinct: true)
respond_with @isbns
end
这是application_helper,其build_grouping为:
def setup_search_form(builder)
fields = builder.grouping_fields builder.object.new_grouping, object_name: 'new_object_name', child_index: "new_grouping" do |f|
render('grouping_fields', f: f)
end
以下是表格:
<%= search_form_for @search, url: advanced_search_isbns_path, html: {method: :post} do |f| %>
<% setup_search_form f %>
#...
<%= render 'results' %>
这是追踪:
Processing by IsbnsController#advanced_search as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"P074clk3UDe3cNNEG6IZ6TXm2q+fr8dMJfJwSBSx/1c=", "q"=>{"g"=>{"0"=>{"m"=>"or"}}}, "commit"=>"Search"}
这是search_form_for的来源:
# File 'lib/ransack/helpers/form_helper.rb', line 4
def search_form_for(record, options = {}, &proc)
if record.is_a?(Ransack::Search)
search = record
options[:url] ||= polymorphic_path(search.klass)
elsif record.is_a?(Array) && (search = record.detect {|o| o.is_a?(Ransack::Search)})
options[:url] ||= polymorphic_path(record.map {|o| o.is_a?(Ransack::Search) ? o.klass : o})
else
raise ArgumentError, "No Ransack::Search object was provided to search_form_for!"
end
options[:html] ||= {}
html_options = {
:class => options[:as] ? "#{options[:as]}_search" : "#{search.klass.to_s.underscore}_search",
:id => options[:as] ? "#{options[:as]}_search" : "#{search.klass.to_s.underscore}_search",
:method => :get
}
options[:as] ||= 'q'
options[:html].reverse_merge!(html_options)
options[:builder] ||= FormBuilder
form_for(record, options, &proc)
end
以下是搜索的来源:
# File 'lib/ransack/search.rb', line 16
def initialize(object, params = {}, options = {})
params ||= {}
@context = Context.for(object)
@context.auth_object = options[:auth_object]
@base = Nodes::Grouping.new(@context, 'and')
build(params.with_indifferent_access)
end
事实上,here's the whole chuffing lot.
Plus:模型的所有路线,包括完整性:
resources :isbns, only: :index do
match 'advanced_search' => 'isbns#advanced_search',
on: :collection, via: [:get, :post], as: :advanced_search
end
resources :isbns do
get :profit, :on => :member
get :workings, :on => :member
put :copyisbn, :on => :member
get :onixxml, :on => :member
collection do
post 'edit_multiple'
put 'update_multiple'
get 'onix'
get 'add_subschemes'
get 'xmp'
get 'profitindex'
delete 'destroy_multiple'
end
end
模型文件非常繁忙,所以要求更多,但这里有一些关联: Isbn.rb: attr_accessible:marketingtexts_attributes,:prices_attributes #etc has_many:marketingtexts,:dependent =&gt; :破坏 has_many:supplies,:dependent =&gt; :破坏
Marketingtext.rb
attr_accessible :user_id, :created_at, :legacycode, :updated_at, :client_id, :isbn_id, #etc
belongs_to :isbn
before_save :map_legacy_codes
validates :client_id, :presence => true
Supply.rb
attr_accessible :user_id, :client_id, :isbn_id, :prices_attributes, #etc
belongs_to :isbn
has_many :supplydetails, :dependent => :destroy
has_many :prices, :through => :supplydetails
accepts_nested_attributes_for :supplydetails
我可以在我的机器上运行克隆的ransack演示代码。在我自己的应用程序中,我已经排除了除jquery和搜索脚本之外的所有javascript。我越来越认为这是与另一个宝石或一些代码的冲突。