A bit lost , could use some direction

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:

<% form_for(:search, :url => searches_path) do |f| %>
<%= text_field(:search,:title) %>
<%= submit_tag “Create” %>

So , yeah I’m pretty much new to setting a search up :slight_smile: and appreciate any suggestions.

TIA
Stuart

Starting over on this post:

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 ?

Stuart

I’m not entire sure what the problem is, although it sounds to me that
what you want to do is somthing like

redirect_to :action => :search, :id => @search.id

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

No, I mean it’s starting to sink in.
Stuart

I don’t know why but I continue to get

You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.to_sym

At this point all I’m trying to do is print out one of the values. In the view I have
<%= @search.category_id %>

And getting this nil.to_sym. Not sure what it means .

Stuart

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?

Dark Ambient wrote:

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.

Stuart

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.

Dark Ambient wrote:

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.

Stuart