I’m working on a database search and using a search model object.
Currently, the searches/new.rhtml works well as far as saving the parameters to the searches table. I think the query is not really working and when I redirect back to list the results all I get is a nil.to_sym error.
Let me show my code, first in the controller:
def create
@search = Search.new(params[:search]) # this saves the selected parameters to the database
#all this pq stuff is really just find statements, however I’m using a search plugin called criteria_query
#but all these pq statement translate to find
pq = Position.query
pq.category_id_eq
(params[:category_id]) if params[:category_id]
pq.state_id_eq(params[:state_id]) if params[:state_id]
pq.term_id_eq(params[:term_id]) if params[:term_id]
pq.title_eq(params[:title]) if params[:title]
pq.city_eq(params[:city]) if params[:city]
# btw , this is a restful setup
respond_to do |format|
if @search.save
flash[:notice] = 'Position was successfully created.' # so it is saving correctly
format.html { redirect_to searches_url(pq) } # here i'm redirecting back to the index url
#here i’m just leaving out the other format code (xml, etc)
end
I’m a bit unsure of how to pass the pq parameters to the index , and wonder if I’ve set the form up correctly.
As an example here is one element from the form and the form tags:
I’m trying to set up a database search. My controllers name is Searches and the model is Search.
I’m using a restful design.
In - def create
@search = Search.new(params[:search])
…
end
This captures the parameters to the database as a saved search. That’s working, but after save I am redirecting back to the index page and want only to display what’s been asked for in the search form. I’m unsure though how to grab those parameters.
Can I pass the @search params back to the index page and use them in a select statement ?
Also, do I have to do anything special in my form ?
Well the action is not search because I’m using a restful controller so it’s the GET method on the index action which I can call
with “redirect_to searches_url(@search)”.
and then in your search action
@search = Search.find params[:id}
#do something with @search
That said I’m not sure whether I’m telling you something you already
know (sorry if I am) or not
Could it be that a redirect is not what you want? Typically, one would do
something like:
def search
form_search_query(params[:search]) # magic code to turn user input into
query
# Assuming the action you really wanted was 'list' only filtered ...
list
render :action => 'list'
end
Now, adding REST into the mix complicates things, but not so much that it's
unworkable. If you refactor the guts out of your list action, you can do
something like:
...
list_setup
render :action => 'list'
BTW: The nil.to_sym issue is related to @search having a nil value. Add this
line to verify that:
logger.debug "oh, noooo, mr. bill, @search is nil." unless @search
Could it be that a redirect is not what you want? Typically, one would do
something like:
def search
form_search_query(params[:search]) # magic code to turn user input into
query
Assuming the action you really wanted was ‘list’ only filtered …
list
render :action => ‘list’
end
Now, adding REST into the mix complicates things, but not so much that it’s
unworkable. If you refactor the guts out of your list action, you can do
something like:
…
list_setup
render :action => ‘list’
BTW: The nil.to_sym issue is related to @search having a nil value. Add this
line to verify that:
logger.debug “oh, noooo, mr. bill, @search is nil.” unless @search
Am I on the right track?
Your on a better track then I am , that’s for certain. I’m not entirely sure though about the nil.to_sym error. I found one open ticket on trac (
dev.rubyonrails.org). Anyway it maybe related to the route I’m attempting to pass @search to. If I say
{ redirect_to searches_url(@search)} it throws an error but
{ redirect_to search_url(@search)} takes me to show and @search is alive there. Anyway I’ll have to hit this again tomorrow and see where your ideas can lead me. Though I was under the assumption that index and list were basically the same.
I'm sorry I mixed things up by using 'list' when you used 'index'. Most
applications use the 'list' action to display ... um, lists. Many have an
index formed as:
def index
list
render :action => 'list'
end
So, unless your index is more of a splash screen, they may be synonymous.
Doesn’t seem to help, unless I break away from the CRUD create a seperate method, though I haven’t tried it yet and not sure I want to see that goes outside of REST.
There is no list in REST controller actions. Not sure how to even implement it.