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.
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.