AWDWR (latest) searc example with REST

There’s an example in the book that presents a search form , listing users and their favorite languages below it. Using the search form text field you can filter the list by their favorite language.

I did the demo from the book using the same schema and it works.

However, I’m trying to implement this same thing in my app which uses REST controllers, and am running into an error when loading the search form. The error is:

ActiveRecord::RecordNotFound in PositionsController#show

Couldn’t find Position with ID=search_position

I’m assuming the issue is in the search action in the controller. The list.rhtml should return all the positions. It all looks pretty straight forward. In the controller:

def search unless params[:search].blank? @position_pages, @positions = paginate :positions, :per_page => 10,

  :order => order_from_params,
  :conditions => Position.conditions_by_like(params[:search])
 [logger.info](http://logger.info) @positions.size
else
  list
end

render :partial => 'search', :layout => false
end

I’m showing the get/show in the controller - though I’m not sure if or why it would intefere with the search action. def show @position = Position.find(params[:id])

respond_to do |format|
  format.html # show.rhtml
  format.xml  { render :xml => @position.to_xml }
end

end

Stuart – http://en.wikipedia.org/wiki/Dark_ambient

I was having a problem like this recently. I believe it's a problem in your routing, because params[:search] is returning 'search_position' instead of the value you want. Have a look at the params that are being received by your action when you call the URL.

Jason

Dark Ambient wrote:

I was having a problem like this recently. I believe it’s a problem in your routing, because params[:search] is returning ‘search_position’ instead of the value you want. Have a look at the params that are being

received by your action when you call the URL.

Jason

Not entirely sure what I’m seeing in the log but this is the select statement going out: Parameters: {“action”=>“show”, “id”=>“search_position”, “controller”=>“positions”}

[4;36;1mUser Columns (0.000000)[0m [0;1mSHOW FIELDS FROM users[0m [4;35;1mUser Load (0.000000)[0m [0mSELECT * FROM users WHERE (users.id = 11 ) LIMIT 1[0m [4;36;1mPosition Columns (0.000000)[0m [0;1mSHOW FIELDS FROM positions[0m

[4;35;1mPosition Load (0.000000)[0m [0mSELECT * FROM positions WHERE (positions.id = 0) [0m

In the def index action I am filtering show by user. I guess at this point that is just an aside since /positions will return all the records of that user and I also can remove that condition. Which didn’t help.

What I see here is the odd statement "WHERE (positions.id = 0)

Not sure how the routing would effect things. Currently my routes.rb are fairly simple.

map.resources :users, :sessions, :companies map.resources :positions, :member => {:description => :get}

Stuart

What I’m seeing here ( i think) is that even though it should be accessing the search action in the controllers, it’s calling the get / show action. Hence the problem with the id issue.

Anyone ? This might be a routing issue though right now nothing I’ve tried in routes.rb is helping.

Stuart

I took the whole code and moved it out of my restful controller, since I can’t quite figure out how to make it work in there. However now that it has it’s own controller, I’m getting a nil error. Here is the code:

Controller code: class PositionController < ApplicationController
def search unless params[:search].blank? @position_pages, @position = paginate(:positions, :per_page => 10, :order => order_from_params, :conditions => Position.conditions(params[:search])) logger.info @positions.size else list end render :partial => ‘search’, :layout => false

end

end

The search view <%= start_form_tag(‘javascript:void%200’) %>

Listing users <%= text_field_tag 'search', @search %>

<%= observe_field :search,
:frequency => 0.5,
:update => ‘ajaxWrapper’, :complete => “Element.hide(‘spinner’)”, :before => " Element.show(‘spinner’)", :with => “‘search=’ + encodeURIComponent(value)”, :url => {:action=>‘search’, :only_path => false} %>

<%= end_form_tag %>
<%= render :partial=>'search' %>

the _search partial

<%= javascript_include_tag :defaults %>

<% for position in @positions %> <----- this is where I’m getting the nil object. Makes no sense.

<% end %>

<%= link_to ‘Previous page’, { :page => @position_pages.current.previous } if @position_pages.current.previous %> <%= link_to ‘Next page’, { :page => @position_pages.current.next } if @position_pages.current.next %>

Stuart

Position Title
<%=h [position.company.name](http://position.company.name) %> <%=h position.title %> <%= link_to "Show", search_position_path(position) %>