views:

191

answers:

1

To preface this I am a complete Rails newcomer so don't judge my lack of skills too harshly. Anyway, my rails app is throwing this error:

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

I am trying to make a basic form that will allow a user to search for a "match" by "country". It is just a proof of concept as I am still learning.

Here is my model:

class OmMatch < ActiveRecord::Base
end

Here is my controller:

class OmMatchesController < ApplicationController

 def search
  @search = OmMatch.search(params[:search])
  @match = @search.all
 end 
end

Here is the view:

<html> 
  <head><title>"Matches"</title></head>
  <body>
    <% form_for @search do |f| %>
    <p>
      <%= f.label :country_equals, "Country" %><br />
      <%= f.text_field :country_equals %>
    </p>

    <p>
      <%= f.submit "Submit" %>
    </p>

  <% end %>

  <table>
  <tr>
    <th>"Match Name"</th>
    <th>"Country"</th>
  </tr>
  <% @match.each do |match| %>
  <tr>
    <td><%=h match.matchname %></td>
    <td><%=h match.country %></td>
  </tr>
 <% end %>
 <table>
</body>
</html>

I believe that the problem is from search not being initialized but I am not sure how to that.

Thanks

+1  A: 

When you do

<% form_for @search do |f| %>

It is expecting @search to be an initialized active record object with routes defined in the routes.rb file.

I assume the problem you are having is when doing a GET request to the OmMatch search action.

If search is an object you can initialize, then just add

@search = Search.new

to your controller.

You need to consider the workflow here for what action is run when the form is requested via a link, and what occurs when a user posts the form.

Remember that you can run

rake routes

to see all the routes that your application knows about for your resources and controllers.

I would recommend that you check out the railscasts screen casts, especially the ones on routing. It is very important that you understand how your controller code is mapped by urls. http://railscasts.com/episodes?search=routing Once you have your routes mapped correctly and you understand when each action will be invoked, you can ensure that your objects are created before you need them.

danivovich
Sorry it has been a few days. I am getting back at this problem now but I am still struggling. I added map.connect ':controller/:action/'to my routes.rb file so that I could browse to om_matches/search. I also added the line @search = Search.newto the search action in the OmMatchesController. I am now getting the following error uninitialized constant OmMatchesController::SearchI am very confused right now. Do you I need to declare the variable outside of the action in the controller or make a new model or something?
jpdbaugh
I added some info to the end of my answer, if you need more help please post what URL you are accessing and more info about your routes and controller actions.
danivovich