model class inheritance (problem with view)
I have 2 model classes with different attributes:
class User < ActiveRecord::Base
end
class Subuser < User
end
When I call the 'new' function within controller:
def new
@subuser = Subuser.new
respond_to do |开发者_Go百科format|
format.html # new.html.erb
format.xml { render :xml => @hosting }
end
end
and try to access data with view thats held by the Subuser model (migration):
<% form_for(@subuser) do |f| %>
<%= f.text_field :subname %>
<% end %>
I get:
undefined method `subname' for #
However, if I change it to some other cell that is defined under User model/migration, it will all work okay.
It seems like there are some isssues with the data access.
What am I doing wrong?
Thanks for help!
I get the following error
From the code posted, @hosting
is only defined for format=xml
, it won't exist for the HTML view.
What is the @hosting variable supposed to be? Mike is right, you aren't defining it anywhere.
My guess is that you probably want to do something more along the lines of
form_for(@subuser) do |f|
Are you sure that you are properly using STI (single table inheritance) here? What is your schema for the users table? Are you saying that you have a migration for Subuser... because if you are trying to use STI, it should use the users
table with a type
column.
Your sub model only knows what it is given via it's parent. So if your trying to access an "imaginary" attribute you will get that undefined method error.
Try added attr_accessor :subname
to give it that attribute.
I'm also a bit curious about that @hosting thing...
精彩评论