hellomello hellomello - 6 months ago 34
Ruby Question

rails 4: creating custom search form with custom routes

I think this should be an easy problem, but I just can't figure it out right now. Been coding all day, need another set of eyes.

Current URL is being displayed:

http://localhost:3000/q?utf8=✓&query%5Bquery%5D=testing


as opposed to

http://localhost:3000/q?query=testing


routes.rb

Rails.application.routes.draw do

resources :biz do
member do
get 'photo'
end
end
get '/q' => 'search#index', as: :search


root 'welcome#index'

end


welcome/index.haml

= simple_form_for :query, url: search_path, method: "get" do |q|
= q.input :query


I have a search_controller.rb

class SearchController < ApplicationController
def index
if params.has_key?(:query)
@query = Model.q(params[:query])
else
redirect_to root_path
end
end
end


What should I need to fix or add?

Form from inspector

<form novalidate="novalidate" class="simple_form /q" action="/q" accept-charset="UTF-8" method="get">
<input name="utf8" type="hidden" value="✓">
<div class="form-group string required query_query">
<label class="string required control-label" for="query_query">
<abbr title="required"></abbr> Query
</label>
<input class="string required form-control" type="text" name="query[query]" id="query_query">
</div>
</form>

Answer

remove the /:query as your sending it as a query param not a path param. make it /q

Also form might have to be

= simple_form_for :search, url: search_path do |q|

The form will submit http://example.com/q?query=this not http://example.com/q/this/

URL issue,

The problem is the simple form is wrapping it in a model. So your params would be params[:query][:query] for the input. You can just you text_field_tag 'query' instead of q.input :query

Comments