我正在尝试使用ransack在rails上创建搜索表单。我遇到了一个小问题。
我收到错误
param丢失或值为空:信息
当我提交搜索时。
Information_controller.rb
class InformationController < ApplicationController
before_action :set_information, only: [:show, :edit, :update, :destroy]
respond_to :html
# Add the breadcrumbs
require 'uri'
# User must authenticate to use all actions in the users controller
before_filter :authenticate_user!, except: [ :home, :show, :splash]
def index
@search = Information.search(params[:q])
@information = params[:distinct].to_i.zero? ?
@search.result :
@search.result(distinct: true)
@information = @information.page(params[:page]).per(10)
respond_with @information
end
def admin
@search = Information.search(params[:q])
@information = @search.result
@search.build_condition
end
#def search
#@q = Information.ransack(params[:q])
#@information = @q.result(distinct: true)
#end
# GET /information/1
# GET /information/1.json
def show
session[:return_to] = request.fullpath
end
# GET /information/new
def new
@information = Information.new
end
# GET /information/1/edit
def edit
end
# POST /information
# POST /information.json
def create
@information = Information.new(information_params)
respond_to do |format|
if @information.save
format.html { redirect_to @information, notice: 'Entry was successfully created.' }
format.json { render :show, status: :created, location: @information }
else
format.html { render :new }
format.json { render json: @information.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /information/1
# PATCH/PUT /information/1.json
def update
respond_to do |format|
if @information.update(information_params)
format.html { redirect_to @information, notice: 'Entry was successfully updated.' }
format.json { render :show, status: :ok, location: @information }
else
format.html { render :edit }
format.json { render json: @information.errors, status: :unprocessable_entity }
end
end
end
# DELETE /information/1
# DELETE /information/1.json
def destroy
@information.destroy
respond_to do |format|
format.html { redirect_to information_index_url, notice: 'Entry was successfully removed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_information
@information = Information.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def information_params
params.require(:information).permit(:name, :description, :show, :date, :heading, :subheading, :image, :places, :data_type, :reference)
end
end
搜索表单
<%= search_form_for(
@search,
:url => admin_index_path,
html: { method: :post }
) do |f| %>
<%= f.condition_fields do |c| %>
<%= render "condition_fields", f: c%>
<% end %>
<p><%= link_to_add_fields "Add Conditions", f, :condition %>
<div class="actions"><%= f.submit "Search" %></div>
<% end %>
路由
Rails.application.routes.draw do
root :to => 'information#splash'
devise_for :users, :skip => [:sessions], :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
as :user do
get 'signin' => 'devise/sessions#new', :as => :new_user_session
post 'signin' => 'devise/sessions#create', :as => :user_session
delete 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
end
##########################################
# ADMIN #
##########################################
#admin panel
get '/admin' => 'information#admin'
#get '/admin/new' => 'information'
resources :information, path: '/admin'
get '/home/:id', to: 'information#show'
get 'home' => 'information#index', :as => :home
concern :paginatable do
get '(page/:page)', :action => :index, :on => :collection, :as => ''
end
concern :paginatable do
get '(page/:page)', :action => :information, :on => :collection, :as => ''
end
#get '/new' => 'information#new'
#get '/public' => 'information#'
##########################################################
resources :information
resources :admin
end
谢谢。如果您需要更多详细信息,请询问:^)