NilClass passed to partial view

Hi,
I have a simple program that tries to do something really basic.
First, I have a company Model :

Hi,
I have a simple program that tries to do something really basic.
First, I have a company Model :
-------------------------------------
class Company < ActiveRecord::Base
has_one :billingAddress, :foreign_key=>"id", :class_name=>"Address"
end

Then I have the address Model :
--------------------------------------
class Address< ActiveRecord::Base
belongs_to :company
end

I've created manually an entry in the DB for Address and Company and
I've linked them (Company table has a "billingAddress_id" row)

In my views "company/_form"(scaffold generated), I want to display the
address form. So I added (following Rails Guides) the following line to
my view :

<%= render :partial => "addresses/edit", :locals => { :shippingAddress
=> @address} %>
It keeps saying : "undefined method `model_name' for NilClass:Class "
tried this :

In the partial have you got something.model_name? If so then
'something' is nil.

In the traceback when you get the error message you should be able to
see the line that is failing.

Colin

Colin Law wrote:

Colin Law wrote:

class Address< ActiveRecord::Base
<%= render :partial => "addresses/edit", :locals => { :shippingAddress
=> @address} %>
It keeps saying : "undefined method `model_name' for NilClass:Class "
tried this :

In the partial have you got something.model_name? If so then
'something' is nil.

In the traceback when you get the error message you should be able to
see the line that is failing.

Colin

Thanks Colin for the fast answer :

In the partial I have the "standard" scaffolded code for my Address
class

So it's a "form_for @address" just like Rails 3 is generates....

You did not say you are using Rails 3 (unless I missed it). I have no
experience of Rails 3, which is still under development. You did not
answer the question as to whether you are accessing
something.model_name, nor whether the trace shows the failing line.

You do not specify your knowledge level, if you are just starting I
would not recommend rails 3. You will never know whether a problem is
down to your experience or a problem with rails itself.

Colin

I think your problem is in :foreign_key=>"id" part. This means that
foreign key to address is named 'id' which is actually id column for
Company model. Remove this part if your foreign key column is
'address_id' or use real column name, i.e. 'billing_address_id'
otherwise

Regards,
Bosko

You did not say you are using Rails 3 (unless I missed it). I have no
experience of Rails 3, which is still under development. You did not
answer the question as to whether you are accessing
something.model_name, nor whether the trace shows the failing line.

You do not specify your knowledge level, if you are just starting I
would not recommend rails 3. You will never know whether a problem is
down to your experience or a problem with rails itself.

Colin

Hi Colin,
Yes I'm using Rails 3. My knowledge level is ... well, begginer since
I'm not able to make this thing works :slight_smile: My years of experience are
with PHP and .Net. I'm trying Rails because it looks interesting and
promising (once I'll get a solid grip on basic things like this!)

Here is my partial view for the Address class.
1<%= form_for(@address) do |f| %>
2 <div class="field">
3 <%= f.label :city %><br />
4 <%= f.text_field :city %>
5 </div>
6
7<% end %>

The failling line is #3. I'm able to load the partial if I put in it a
string and no code (like "<h1> TEST </h1>"). I'm not sure that I
understand what you meant by "something.model_name" ... As for Rails 3,
they said on guides.rails.info :

"But if you’re starting development on a new application and you don’t
mind getting wind in your hair, please do jump on board!" I'm starting
a new app and I'm eager to learn so that's what I've done.... is it a
choice that bad ?

Thank you for you replies !
Greg

You did not say you are using Rails 3 (unless I missed it). I have no
experience of Rails 3, which is still under development. You did not
answer the question as to whether you are accessing
something.model_name, nor whether the trace shows the failing line.

You do not specify your knowledge level, if you are just starting I
would not recommend rails 3. You will never know whether a problem is
down to your experience or a problem with rails itself.

Colin

Hi Colin,
Yes I'm using Rails 3. My knowledge level is ... well, begginer since
I'm not able to make this thing works :slight_smile: My years of experience are
with PHP and .Net. I'm trying Rails because it looks interesting and
promising (once I'll get a solid grip on basic things like this!)

Here is my partial view for the Address class.
1<%= form_for(@address) do |f| %>
2 <div class="field">
3 <%= f.label :city %><br />
4 <%= f.text_field :city %>
5 </div>
6
7<% end %>

The failling line is #3. I'm able to load the partial if I put in it a
string and no code (like "<h1> TEST </h1>"). I'm not sure that I
understand what you meant by "something.model_name"

I think Bosko may well have the answer. I was assuming that one of
your models had a file model_name but I see now that this is the rails
model it is talking about.

... As for Rails 3,

they said on guides.rails.info :

"But if you’re starting development on a new application and you don’t
mind getting wind in your hair, please do jump on board!" I'm starting
a new app and I'm eager to learn so that's what I've done.... is it a
choice that bad ?

I think this may have been intended for experienced users starting a
new app rather than beginners. You will not find many tutorials and
guides for rails 3 yet. I would consider going back to 2.3.x and
starting with the guides at http://guides.rubyonrails.org/

Colin

@Bosko : I'll try your suggestion tonight and keeps you posted.

@Collin : Thanks for your time... I'll check for rails 2.3.x and if I'm
not able to make this work, I may roll back

Thank you guys for your help !