Hi guys,
Quick question. Here is my code:
#routes
map.resources :customers, :has_many => [:addresses, :matchings]
map.connect ":controller/:action/:id"
#url path:
http://127.0.0.1:3000/customers/index/3
#customers controller
def index
@customer = Customer.find(params[:id])
end
#customers view/index.html.erb
...
<%= @customer.name %>
...
Error: undefined method `name' for nil:NilClass.
Here's my reasoning. The parameter :id is coming from my url path (i.e. we're looking for customer #3 in the above path). @customer should find that array easily, then @customer.name should produce the name, but apparently @customer is blank. Why?
I assume the problem is that I'm not producing an array in my controller?
------UPDATE------
Hi Larry, thanks very much for the comments.
What I mean by 'index' is actually a home page that customers will hit when they log in to the site. (I also have separate logins for other users like employers and they go to their own index). I'm currently using 'list' as a list of customers and 'show' for one individual... is there a better way?
Second and MOST important: the logs are not giving me the answer to the fundamental problem, which is the inability to render the page.
Processing CustomersController#index (for 127.0.0.1 at 2010-05-16 16:56:19) [GET]
Parameters: {"action"=>"index", "id"=>"10", "controller"=>"customers"}
...
[4;36;1mCustomer Load (2.0ms)[0m [0;1mSELECT * FROM "customers" WHERE ("customers"."id" = 10) [0m
[4;35;1mMatching Load (24.0ms)[0m [0mSELECT * FROM "matchings" WHERE ("matchings".customer_id = 10) [0m
Completed in 153ms (View: 18, DB: 26) | 200 OK [http://127.0.0.1/customers/index/10]
------UPDATE #2 (last!) ------
I read through all of your comments and they have helped me not only make progress on my problem but also learn more about how the pieces fit together. For that, thanks everyone.
Alex is right - my routes are not lining up how they need to. I created a new action in my customer controller called Home. This is the customer's home base when they log in (it doesn't display info about one customer so it probably shouldn't be show, and it isn't a list so shouldn't be index or list).
Rake routes # ...gives me a list of routes that does NOT include home. Why?
Second, if the route displayed has, for example:
:action => "home"
/customers # (instead of what I need, '/customers/:id')
what do I have to do to change that route to accept an :id?