<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Nested Has_one Relationship with Fields_for and Attr_accessible in Model Class</title>
	<atom:link href="http://seanbehan.com/ruby-on-rails/nested-has_one-relationship-with-fields_for-and-attr_accessible-in-model-class/feed/" rel="self" type="application/rss+xml" />
	<link>http://seanbehan.com/ruby-on-rails/nested-has_one-relationship-with-fields_for-and-attr_accessible-in-model-class/</link>
	<description>Web Programming, Ruby on Rails, Wordpress, PHP from Burlington, Vermont</description>
	<lastBuildDate>Mon, 30 Jan 2012 12:48:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Giovanni</title>
		<link>http://seanbehan.com/ruby-on-rails/nested-has_one-relationship-with-fields_for-and-attr_accessible-in-model-class/#comment-1318</link>
		<dc:creator>Giovanni</dc:creator>
		<pubDate>Tue, 21 Jun 2011 09:00:09 +0000</pubDate>
		<guid isPermaLink="false">http://seanbehan.com/?p=457#comment-1318</guid>
		<description>Thanks.</description>
		<content:encoded><![CDATA[<p>Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sean Behan</title>
		<link>http://seanbehan.com/ruby-on-rails/nested-has_one-relationship-with-fields_for-and-attr_accessible-in-model-class/#comment-55</link>
		<dc:creator>Sean Behan</dc:creator>
		<pubDate>Tue, 01 Jun 2010 14:02:24 +0000</pubDate>
		<guid isPermaLink="false">http://seanbehan.com/?p=457#comment-55</guid>
		<description>Here is another example model using restful authentication http://pasite.org/code/569 w/all validations removed. to debug, you may want to comment out all of the validations in your model and focus on the root of the problem. remove anything that you see as extraneous to the bug.

looking at your code again, i don&#039;t see the

has_one :profile

declaration. you need this to make the association work, which would explain why the profile_id is failing to be populated.

the important parts are


has_one :profile
accepts_nested_attributes_for :profile
attr_accessible :profile_attributes

# - minus &lt; &amp; &gt; tags for this erb code
f.fields_for :profile do &#124;profile&#124;
  profile.text_field :favorite_color %&gt;
end


thanks for the comments... i enjoy them. also, you may want to take a look at AuthLogic. It is the new standard for Rails authentication plugins. http://railscasts.com/episodes/160-authlogic Ryan Bates has a great screencast for setting it up.</description>
		<content:encoded><![CDATA[<p>Here is another example model using restful authentication <a href="http://pasite.org/code/569" rel="nofollow">http://pasite.org/code/569</a> w/all validations removed. to debug, you may want to comment out all of the validations in your model and focus on the root of the problem. remove anything that you see as extraneous to the bug.</p>
<p>looking at your code again, i don&#8217;t see the</p>
<p>has_one :profile</p>
<p>declaration. you need this to make the association work, which would explain why the profile_id is failing to be populated.</p>
<p>the important parts are</p>
<p>has_one :profile<br />
accepts_nested_attributes_for :profile<br />
attr_accessible :profile_attributes</p>
<p># &#8211; minus &lt; &amp; &gt; tags for this erb code<br />
f.fields_for :profile do |profile|<br />
  profile.text_field :favorite_color %&gt;<br />
end</p>
<p>thanks for the comments&#8230; i enjoy them. also, you may want to take a look at AuthLogic. It is the new standard for Rails authentication plugins. <a href="http://railscasts.com/episodes/160-authlogic" rel="nofollow">http://railscasts.com/episodes/160-authlogic</a> Ryan Bates has a great screencast for setting it up.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Techism</title>
		<link>http://seanbehan.com/ruby-on-rails/nested-has_one-relationship-with-fields_for-and-attr_accessible-in-model-class/#comment-54</link>
		<dc:creator>Techism</dc:creator>
		<pubDate>Mon, 31 May 2010 22:59:26 +0000</pubDate>
		<guid isPermaLink="false">http://seanbehan.com/?p=457#comment-54</guid>
		<description>Hmmm. Sorry for choding up your comments with this...looks like pastie for me.  (neat resource BTW!)

Can&#039;t seem to post the script or links, so?</description>
		<content:encoded><![CDATA[<p>Hmmm. Sorry for choding up your comments with this&#8230;looks like pastie for me.  (neat resource BTW!)</p>
<p>Can&#8217;t seem to post the script or links, so?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Techism</title>
		<link>http://seanbehan.com/ruby-on-rails/nested-has_one-relationship-with-fields_for-and-attr_accessible-in-model-class/#comment-53</link>
		<dc:creator>Techism</dc:creator>
		<pubDate>Mon, 31 May 2010 22:48:12 +0000</pubDate>
		<guid isPermaLink="false">http://seanbehan.com/?p=457#comment-53</guid>
		<description>Well...turns out it just SEEMED to be working.  I get the form, validations work, but when they are all passed and the @user is saved, the profile_id is blank and no profile is saved.  Quite a mystery to me.

I&#039;ll try to share my code with &quot;pre&quot; tags here.  If that fails, I try that pastie solution...


# in models/user.rb
require &#039;digest/sha1&#039;

class User  :destroy
  accepts_nested_attributes_for :profile, :allow_destroy =&gt; true

  # has_role? simply needs to return true or false whether a user has a role or not.
  # It may be a good idea to have &quot;admin&quot; roles return true always
  def has_role?(role_in_question)
    @_list &#124;&#124;= self.roles.collect(&amp;:name)
    return true if @_list.include?(&quot;admin&quot;)
    (@_list.include?(role_in_question.to_s) )
  end
  # ---------------------------------------
  include Authentication
  include Authentication::ByPassword
  include Authentication::ByCookieToken
  include Authorization::StatefulRoles
  # validates_presence_of     :login
  # validates_length_of       :login,    :within =&gt; 3..40
  # validates_uniqueness_of   :login
  # validates_format_of       :login,    :with =&gt; Authentication.login_regex, :message =&gt; Authentication.bad_login_message

  validates_format_of       :name,     :with =&gt; Authentication.name_regex,  :message =&gt; Authentication.bad_name_message, :allow_nil =&gt; true
  validates_length_of       :name,     :maximum =&gt; 100

  validates_presence_of     :email
  validates_length_of       :email,    :within =&gt; 6..100 #r@a.wk
  validates_uniqueness_of   :email
  validates_format_of       :email,    :with =&gt; Authentication.email_regex, :message =&gt; Authentication.bad_email_message

  #validates_presence_of     :company, :address, :country, :company_type, :phone, :fax, :end_product, :end_product_use, :end_product_categories, :end_user_countries, :statement_name, :statement_signature, :statement_title

  # HACK HACK HACK -- how to do attr_accessible from here?
  # prevents a user from submitting a crafted form that bypasses activation
  # anything else you want your user to change should be added here.
  #attr_accessible :profile_attributes, :profile, :login, :email, :name, :password, :password_confirmation#, :company, :address, :country, :company_type, :phone, :fax, :end_product, :end_product_use, :end_product_categories, :end_user_countries, :statement_name, :statement_signature, :statement_title, :end_use_production_date, :website, :ultimate_consignee_name, :ultimate_consignee_address, :ultimate_consignee_type, :ultimate_consignee_website

  # Authenticates a user by their login name and unencrypted password.  Returns the user or nil.
  #
  # uff.  this is really an authorization, not authentication routine.
  # We really need a Dispatch Chain here or something.
  # This will also let us return a human error message.
  #
  def self.authenticate(email, password)
    u = find_in_state :first, :active, :conditions =&gt; {:email =&gt; email} # need to get the salt
    u &amp;&amp; u.authenticated?(password) ? u : nil
  end

  def self.legacy(email, password)
    u = LegacyUser.find(:first, :conditions =&gt; {:email =&gt; email, :password =&gt; password})
    u ? u : nil
  end

  def login=(value)
    write_attribute :login, (value ? value.downcase : nil)
  end

  def email=(value)
    write_attribute :email, (value ? value.downcase : nil)
  end

  protected

    def make_activation_code
        self.deleted_at = nil
        self.activation_code = self.class.make_token
    end
end



# in models/profile.rb
class Profile &lt; ActiveRecord::Base
  belongs_to :user
  validates_presence_of     :address, :company,
  :country, :company_type, :phone,
  :statement_name, :statement_signature, :statement_title,
  :end_product_use, :end_use_production_date, :end_product_categories

  # def accessible_to(user_id)
  #   if self.user.id == user_id &#124;&#124; admin?
  #     return true
  #   end
  # end

end



# in views/users/_form.html.erb





 {:action =&gt; &#039;create&#039;} do &#124;f&#124; %&gt;












		&quot;/profiles/form&quot;, :locals=&gt;{:profile_form =&gt; profile_form } %&gt;






 #in controllers/users_controller.rb
  def new
    @user = User.new
    @user.build_profile
  end</description>
		<content:encoded><![CDATA[<p>Well&#8230;turns out it just SEEMED to be working.  I get the form, validations work, but when they are all passed and the @user is saved, the profile_id is blank and no profile is saved.  Quite a mystery to me.</p>
<p>I&#8217;ll try to share my code with &#8220;pre&#8221; tags here.  If that fails, I try that pastie solution&#8230;</p>
<p># in models/user.rb<br />
require &#8216;digest/sha1&#8242;</p>
<p>class User  :destroy<br />
  accepts_nested_attributes_for :profile, :allow_destroy =&gt; true</p>
<p>  # has_role? simply needs to return true or false whether a user has a role or not.<br />
  # It may be a good idea to have &#8220;admin&#8221; roles return true always<br />
  def has_role?(role_in_question)<br />
    @_list ||= self.roles.collect(&amp;:name)<br />
    return true if @_list.include?(&#8220;admin&#8221;)<br />
    (@_list.include?(role_in_question.to_s) )<br />
  end<br />
  # &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
  include Authentication<br />
  include Authentication::ByPassword<br />
  include Authentication::ByCookieToken<br />
  include Authorization::StatefulRoles<br />
  # validates_presence_of     :login<br />
  # validates_length_of       :login,    :within =&gt; 3..40<br />
  # validates_uniqueness_of   :login<br />
  # validates_format_of       :login,    :with =&gt; Authentication.login_regex, :message =&gt; Authentication.bad_login_message</p>
<p>  validates_format_of       :name,     :with =&gt; Authentication.name_regex,  :message =&gt; Authentication.bad_name_message, :allow_nil =&gt; true<br />
  validates_length_of       :name,     :maximum =&gt; 100</p>
<p>  validates_presence_of     :email<br />
  validates_length_of       :email,    :within =&gt; 6..100 #r@a.wk<br />
  validates_uniqueness_of   :email<br />
  validates_format_of       :email,    :with =&gt; Authentication.email_regex, :message =&gt; Authentication.bad_email_message</p>
<p>  #validates_presence_of     :company, :address, :country, :company_type, :phone, :fax, :end_product, :end_product_use, :end_product_categories, :end_user_countries, :statement_name, :statement_signature, :statement_title</p>
<p>  # HACK HACK HACK &#8212; how to do attr_accessible from here?<br />
  # prevents a user from submitting a crafted form that bypasses activation<br />
  # anything else you want your user to change should be added here.<br />
  #attr_accessible :profile_attributes, :profile, :login, :email, :name, :password, :password_confirmation#, :company, :address, :country, :company_type, :phone, :fax, :end_product, :end_product_use, :end_product_categories, :end_user_countries, :statement_name, :statement_signature, :statement_title, :end_use_production_date, :website, :ultimate_consignee_name, :ultimate_consignee_address, :ultimate_consignee_type, :ultimate_consignee_website</p>
<p>  # Authenticates a user by their login name and unencrypted password.  Returns the user or nil.<br />
  #<br />
  # uff.  this is really an authorization, not authentication routine.<br />
  # We really need a Dispatch Chain here or something.<br />
  # This will also let us return a human error message.<br />
  #<br />
  def self.authenticate(email, password)<br />
    u = find_in_state :first, :active, :conditions =&gt; {:email =&gt; email} # need to get the salt<br />
    u &amp;&amp; u.authenticated?(password) ? u : nil<br />
  end</p>
<p>  def self.legacy(email, password)<br />
    u = LegacyUser.find(:first, :conditions =&gt; {:email =&gt; email, :password =&gt; password})<br />
    u ? u : nil<br />
  end</p>
<p>  def login=(value)<br />
    write_attribute :login, (value ? value.downcase : nil)<br />
  end</p>
<p>  def email=(value)<br />
    write_attribute :email, (value ? value.downcase : nil)<br />
  end</p>
<p>  protected</p>
<p>    def make_activation_code<br />
        self.deleted_at = nil<br />
        self.activation_code = self.class.make_token<br />
    end<br />
end</p>
<p># in models/profile.rb<br />
class Profile &lt; ActiveRecord::Base<br />
  belongs_to :user<br />
  validates_presence_of     :address, :company,<br />
  :country, :company_type, :phone,<br />
  :statement_name, :statement_signature, :statement_title,<br />
  :end_product_use, :end_use_production_date, :end_product_categories</p>
<p>  # def accessible_to(user_id)<br />
  #   if self.user.id == user_id || admin?<br />
  #     return true<br />
  #   end<br />
  # end</p>
<p>end</p>
<p># in views/users/_form.html.erb</p>
<p> {:action =&gt; &#8216;create&#8217;} do |f| %&gt;</p>
<p>		&#8220;/profiles/form&#8221;, :locals=&gt;{:profile_form =&gt; profile_form } %&gt;</p>
<p> #in controllers/users_controller.rb<br />
  def new<br />
    @user = User.new<br />
    @user.build_profile<br />
  end</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: admin</title>
		<link>http://seanbehan.com/ruby-on-rails/nested-has_one-relationship-with-fields_for-and-attr_accessible-in-model-class/#comment-52</link>
		<dc:creator>admin</dc:creator>
		<pubDate>Mon, 31 May 2010 19:08:52 +0000</pubDate>
		<guid isPermaLink="false">http://seanbehan.com/?p=457#comment-52</guid>
		<description>Sorry about the code not posting... could be the &lt; and &gt; tags are being stripped... form views/erb often starts w/ those. glad to hear it&#039;s working though</description>
		<content:encoded><![CDATA[<p>Sorry about the code not posting&#8230; could be the &lt; and &gt; tags are being stripped&#8230; form views/erb often starts w/ those. glad to hear it&#8217;s working though</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Techism</title>
		<link>http://seanbehan.com/ruby-on-rails/nested-has_one-relationship-with-fields_for-and-attr_accessible-in-model-class/#comment-51</link>
		<dc:creator>Techism</dc:creator>
		<pubDate>Mon, 31 May 2010 19:05:15 +0000</pubDate>
		<guid isPermaLink="false">http://seanbehan.com/?p=457#comment-51</guid>
		<description>Hmm...Never mind.  I figured out what was wrong and couldn&#039;t post my code anyway for some reason.

Good blog post though!</description>
		<content:encoded><![CDATA[<p>Hmm&#8230;Never mind.  I figured out what was wrong and couldn&#8217;t post my code anyway for some reason.</p>
<p>Good blog post though!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: admin</title>
		<link>http://seanbehan.com/ruby-on-rails/nested-has_one-relationship-with-fields_for-and-attr_accessible-in-model-class/#comment-50</link>
		<dc:creator>admin</dc:creator>
		<pubDate>Mon, 31 May 2010 18:26:51 +0000</pubDate>
		<guid isPermaLink="false">http://seanbehan.com/?p=457#comment-50</guid>
		<description>The fields_for method is not on the model... it is a form helper method. what does your form/view look like?

use the &quot;pre&quot; tag instead of code... but if you have a lot of code and code that uses &lt; and &gt; characters, it&#039;s easier to use a pastie http://pastie.org and share the link</description>
		<content:encoded><![CDATA[<p>The fields_for method is not on the model&#8230; it is a form helper method. what does your form/view look like?</p>
<p>use the &#8220;pre&#8221; tag instead of code&#8230; but if you have a lot of code and code that uses &lt; and &gt; characters, it&#8217;s easier to use a pastie <a href="http://pastie.org" rel="nofollow">http://pastie.org</a> and share the link</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Techism</title>
		<link>http://seanbehan.com/ruby-on-rails/nested-has_one-relationship-with-fields_for-and-attr_accessible-in-model-class/#comment-49</link>
		<dc:creator>Techism</dc:creator>
		<pubDate>Mon, 31 May 2010 17:47:41 +0000</pubDate>
		<guid isPermaLink="false">http://seanbehan.com/?p=457#comment-49</guid>
		<description>Huh...it scrambled my comment and changed my code in strange places.  Let me try posting that code again:

User Model

&lt;pre&gt;
require &#039;digest/sha1&#039;

class User  :destroy
  #accepts_nested_attributes_for :profile, :allow_destroy =&gt; true

  # has_role? simply needs to return true or false whether a user has a role or not.
  # It may be a good idea to have &quot;admin&quot; roles return true always
  def has_role?(role_in_question)
    @_list &#124;&#124;= self.roles.collect(&amp;:name)
    return true if @_list.include?(&quot;admin&quot;)
    (@_list.include?(role_in_question.to_s) )
  end
  # ---------------------------------------
  include Authentication
  include Authentication::ByPassword
  include Authentication::ByCookieToken
  include Authorization::StatefulRoles
  # validates_presence_of     :login
  # validates_length_of       :login,    :within =&gt; 3..40
  # validates_uniqueness_of   :login
  # validates_format_of       :login,    :with =&gt; Authentication.login_regex, :message =&gt; Authentication.bad_login_message

  validates_format_of       :name,     :with =&gt; Authentication.name_regex,  :message =&gt; Authentication.bad_name_message, :allow_nil =&gt; true
  validates_length_of       :name,     :maximum =&gt; 100

  validates_presence_of     :email
  validates_length_of       :email,    :within =&gt; 6..100 #r@a.wk
  validates_uniqueness_of   :email
  validates_format_of       :email,    :with =&gt; Authentication.email_regex, :message =&gt; Authentication.bad_email_message

  #validates_presence_of     :company, :address, :country, :company_type, :phone, :fax, :end_product, :end_product_use, :end_product_categories, :end_user_countries, :statement_name, :statement_signature, :statement_title

  # HACK HACK HACK -- how to do attr_accessible from here?
  # prevents a user from submitting a crafted form that bypasses activation
  # anything else you want your user to change should be added here.
  attr_accessible :profile_attributes, :profile, :login, :email, :name, :password, :password_confirmation#, :company, :address, :country, :company_type, :phone, :fax, :end_product, :end_product_use, :end_product_categories, :end_user_countries, :statement_name, :statement_signature, :statement_title, :end_use_production_date, :website, :ultimate_consignee_name, :ultimate_consignee_address, :ultimate_consignee_type, :ultimate_consignee_website

  # Authenticates a user by their login name and unencrypted password.  Returns the user or nil.
  #
  # uff.  this is really an authorization, not authentication routine.
  # We really need a Dispatch Chain here or something.
  # This will also let us return a human error message.
  #
  def self.authenticate(email, password)
    u = find_in_state :first, :active, :conditions =&gt; {:email =&gt; email} # need to get the salt
    u &amp;&amp; u.authenticated?(password) ? u : nil

  end

  def self.legacy(email, password)
    u = LegacyUser.find(:first, :conditions =&gt; {:email =&gt; email, :password =&gt; password})
    u ? u : nil
  end

  def login=(value)
    write_attribute :login, (value ? value.downcase : nil)
  end

  def email=(value)
    write_attribute :email, (value ? value.downcase : nil)
  end

  protected

    def make_activation_code
        self.deleted_at = nil
        self.activation_code = self.class.make_token
    end


end
&lt;/pre&gt;

Views/Users/_form.html.erb

&lt;code&gt;




&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Huh&#8230;it scrambled my comment and changed my code in strange places.  Let me try posting that code again:</p>
<p>User Model</p>
<pre class="wp-code-highlight prettyprint">
require 'digest/sha1'

class User  :destroy
  #accepts_nested_attributes_for :profile, :allow_destroy =&amp;gt; true

  # has_role? simply needs to return true or false whether a user has a role or not.
  # It may be a good idea to have &quot;admin&quot; roles return true always
  def has_role?(role_in_question)
    @_list ||= self.roles.collect(&amp;amp;:name)
    return true if @_list.include?(&quot;admin&quot;)
    (@_list.include?(role_in_question.to_s) )
  end
  # ---------------------------------------
  include Authentication
  include Authentication::ByPassword
  include Authentication::ByCookieToken
  include Authorization::StatefulRoles
  # validates_presence_of     :login
  # validates_length_of       :login,    :within =&amp;gt; 3..40
  # validates_uniqueness_of   :login
  # validates_format_of       :login,    :with =&amp;gt; Authentication.login_regex, :message =&amp;gt; Authentication.bad_login_message

  validates_format_of       :name,     :with =&amp;gt; Authentication.name_regex,  :message =&amp;gt; Authentication.bad_name_message, :allow_nil =&amp;gt; true
  validates_length_of       :name,     :maximum =&amp;gt; 100

  validates_presence_of     :email
  validates_length_of       :email,    :within =&amp;gt; 6..100 #r@a.wk
  validates_uniqueness_of   :email
  validates_format_of       :email,    :with =&amp;gt; Authentication.email_regex, :message =&amp;gt; Authentication.bad_email_message

  #validates_presence_of     :company, :address, :country, :company_type, :phone, :fax, :end_product, :end_product_use, :end_product_categories, :end_user_countries, :statement_name, :statement_signature, :statement_title

  # HACK HACK HACK -- how to do attr_accessible from here?
  # prevents a user from submitting a crafted form that bypasses activation
  # anything else you want your user to change should be added here.
  attr_accessible :profile_attributes, :profile, :login, :email, :name, :password, :password_confirmation#, :company, :address, :country, :company_type, :phone, :fax, :end_product, :end_product_use, :end_product_categories, :end_user_countries, :statement_name, :statement_signature, :statement_title, :end_use_production_date, :website, :ultimate_consignee_name, :ultimate_consignee_address, :ultimate_consignee_type, :ultimate_consignee_website

  # Authenticates a user by their login name and unencrypted password.  Returns the user or nil.
  #
  # uff.  this is really an authorization, not authentication routine.
  # We really need a Dispatch Chain here or something.
  # This will also let us return a human error message.
  #
  def self.authenticate(email, password)
    u = find_in_state :first, :active, :conditions =&amp;gt; {:email =&amp;gt; email} # need to get the salt
    u &amp;amp;&amp;amp; u.authenticated?(password) ? u : nil

  end

  def self.legacy(email, password)
    u = LegacyUser.find(:first, :conditions =&amp;gt; {:email =&amp;gt; email, :password =&amp;gt; password})
    u ? u : nil
  end

  def login=(value)
    write_attribute :login, (value ? value.downcase : nil)
  end

  def email=(value)
    write_attribute :email, (value ? value.downcase : nil)
  end

  protected

    def make_activation_code
        self.deleted_at = nil
        self.activation_code = self.class.make_token
    end

end
</pre>
<p>Views/Users/_form.html.erb</p>
<p><code></p>
<p></code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Techism</title>
		<link>http://seanbehan.com/ruby-on-rails/nested-has_one-relationship-with-fields_for-and-attr_accessible-in-model-class/#comment-48</link>
		<dc:creator>Techism</dc:creator>
		<pubDate>Mon, 31 May 2010 17:45:04 +0000</pubDate>
		<guid isPermaLink="false">http://seanbehan.com/?p=457#comment-48</guid>
		<description>I&#039;m having a problem getting this to to work with Restful Authentication too...my error is &quot;undefined method `fields_for&#039; for nil:NilClass&quot;.  I&#039;ve checked, checked, and checked my code again, but can&#039;t see what&#039;s causing this problem!

User model:
&lt;pre&gt;require &#039;digest/sha1&#039;

class User  :destroy
  #accepts_nested_attributes_for :profile, :allow_destroy =&gt; true

  # has_role? simply needs to return true or false whether a user has a role or not.
  # It may be a good idea to have &quot;admin&quot; roles return true always
  def has_role?(role_in_question)
    @_list &#124;&#124;= self.roles.collect(&amp;:name)
    return true if @_list.include?(&quot;admin&quot;)
    (@_list.include?(role_in_question.to_s) )
  end
  # ---------------------------------------
  include Authentication
  include Authentication::ByPassword
  include Authentication::ByCookieToken
  include Authorization::StatefulRoles
  # validates_presence_of     :login
  # validates_length_of       :login,    :within =&gt; 3..40
  # validates_uniqueness_of   :login
  # validates_format_of       :login,    :with =&gt; Authentication.login_regex, :message =&gt; Authentication.bad_login_message

  validates_format_of       :name,     :with =&gt; Authentication.name_regex,  :message =&gt; Authentication.bad_name_message, :allow_nil =&gt; true
  validates_length_of       :name,     :maximum =&gt; 100

  validates_presence_of     :email
  validates_length_of       :email,    :within =&gt; 6..100 #r@a.wk
  validates_uniqueness_of   :email
  validates_format_of       :email,    :with =&gt; Authentication.email_regex, :message =&gt; Authentication.bad_email_message

  #validates_presence_of     :company, :address, :country, :company_type, :phone, :fax, :end_product, :end_product_use, :end_product_categories, :end_user_countries, :statement_name, :statement_signature, :statement_title

  # HACK HACK HACK -- how to do attr_accessible from here?
  # prevents a user from submitting a crafted form that bypasses activation
  # anything else you want your user to change should be added here.
  attr_accessible :profile_attributes, :profile, :login, :email, :name, :password, :password_confirmation#, :company, :address, :country, :company_type, :phone, :fax, :end_product, :end_product_use, :end_product_categories, :end_user_countries, :statement_name, :statement_signature, :statement_title, :end_use_production_date, :website, :ultimate_consignee_name, :ultimate_consignee_address, :ultimate_consignee_type, :ultimate_consignee_website

  # Authenticates a user by their login name and unencrypted password.  Returns the user or nil.
  #
  # uff.  this is really an authorization, not authentication routine.
  # We really need a Dispatch Chain here or something.
  # This will also let us return a human error message.
  #
  def self.authenticate(email, password)
    u = find_in_state :first, :active, :conditions =&gt; {:email =&gt; email} # need to get the salt
    u &amp;&amp; u.authenticated?(password) ? u : nil

  end

  def self.legacy(email, password)
    u = LegacyUser.find(:first, :conditions =&gt; {:email =&gt; email, :password =&gt; password})
    u ? u : nil
  end

  def login=(value)
    write_attribute :login, (value ? value.downcase : nil)
  end

  def email=(value)
    write_attribute :email, (value ? value.downcase : nil)
  end

  protected

    def make_activation_code
        self.deleted_at = nil
        self.activation_code = self.class.make_token
    end


end
&lt;/pre&gt;

Profile Model:
&lt;pre&gt;class Profile &lt; ActiveRecord::Base
  belongs_to :user
end
&lt;/pre&gt;

Views/Users/_form.html.erb</description>
		<content:encoded><![CDATA[<p>I&#8217;m having a problem getting this to to work with Restful Authentication too&#8230;my error is &#8220;undefined method `fields_for&#8217; for nil:NilClass&#8221;.  I&#8217;ve checked, checked, and checked my code again, but can&#8217;t see what&#8217;s causing this problem!</p>
<p>User model:</p>
<pre class="wp-code-highlight prettyprint">require 'digest/sha1'

class User  :destroy
  #accepts_nested_attributes_for :profile, :allow_destroy =&amp;gt; true

  # has_role? simply needs to return true or false whether a user has a role or not.
  # It may be a good idea to have &quot;admin&quot; roles return true always
  def has_role?(role_in_question)
    @_list ||= self.roles.collect(&amp;amp;:name)
    return true if @_list.include?(&quot;admin&quot;)
    (@_list.include?(role_in_question.to_s) )
  end
  # ---------------------------------------
  include Authentication
  include Authentication::ByPassword
  include Authentication::ByCookieToken
  include Authorization::StatefulRoles
  # validates_presence_of     :login
  # validates_length_of       :login,    :within =&amp;gt; 3..40
  # validates_uniqueness_of   :login
  # validates_format_of       :login,    :with =&amp;gt; Authentication.login_regex, :message =&amp;gt; Authentication.bad_login_message

  validates_format_of       :name,     :with =&amp;gt; Authentication.name_regex,  :message =&amp;gt; Authentication.bad_name_message, :allow_nil =&amp;gt; true
  validates_length_of       :name,     :maximum =&amp;gt; 100

  validates_presence_of     :email
  validates_length_of       :email,    :within =&amp;gt; 6..100 #r@a.wk
  validates_uniqueness_of   :email
  validates_format_of       :email,    :with =&amp;gt; Authentication.email_regex, :message =&amp;gt; Authentication.bad_email_message

  #validates_presence_of     :company, :address, :country, :company_type, :phone, :fax, :end_product, :end_product_use, :end_product_categories, :end_user_countries, :statement_name, :statement_signature, :statement_title

  # HACK HACK HACK -- how to do attr_accessible from here?
  # prevents a user from submitting a crafted form that bypasses activation
  # anything else you want your user to change should be added here.
  attr_accessible :profile_attributes, :profile, :login, :email, :name, :password, :password_confirmation#, :company, :address, :country, :company_type, :phone, :fax, :end_product, :end_product_use, :end_product_categories, :end_user_countries, :statement_name, :statement_signature, :statement_title, :end_use_production_date, :website, :ultimate_consignee_name, :ultimate_consignee_address, :ultimate_consignee_type, :ultimate_consignee_website

  # Authenticates a user by their login name and unencrypted password.  Returns the user or nil.
  #
  # uff.  this is really an authorization, not authentication routine.
  # We really need a Dispatch Chain here or something.
  # This will also let us return a human error message.
  #
  def self.authenticate(email, password)
    u = find_in_state :first, :active, :conditions =&amp;gt; {:email =&amp;gt; email} # need to get the salt
    u &amp;amp;&amp;amp; u.authenticated?(password) ? u : nil

  end

  def self.legacy(email, password)
    u = LegacyUser.find(:first, :conditions =&amp;gt; {:email =&amp;gt; email, :password =&amp;gt; password})
    u ? u : nil
  end

  def login=(value)
    write_attribute :login, (value ? value.downcase : nil)
  end

  def email=(value)
    write_attribute :email, (value ? value.downcase : nil)
  end

  protected

    def make_activation_code
        self.deleted_at = nil
        self.activation_code = self.class.make_token
    end

end
</pre>
<p>Profile Model:</p>
<pre class="wp-code-highlight prettyprint">class Profile &amp;lt; ActiveRecord::Base
  belongs_to :user
end
</pre>
<p>Views/Users/_form.html.erb</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: admin</title>
		<link>http://seanbehan.com/ruby-on-rails/nested-has_one-relationship-with-fields_for-and-attr_accessible-in-model-class/#comment-47</link>
		<dc:creator>admin</dc:creator>
		<pubDate>Thu, 18 Feb 2010 15:33:47 +0000</pubDate>
		<guid isPermaLink="false">http://seanbehan.com/?p=457#comment-47</guid>
		<description>With restful authentication there is a chunk of code in the user model

&lt;pre&gt;
  # HACK HACK HACK -- how to do attr_accessible from here?
  # prevents a user from submitting a crafted form that bypasses activation
  # anything else you want your user to change should be added here.
  attr_accessible :login, :email, :name, :password, :password_confirmation
&lt;/pre&gt;

Try adding :profile to the attr_accessible list or... commenting out the attr_accessible line altogether. You can use attr_protected instead and explicitly list attributes unavailable for mas assignment. A blacklist as opposed to a whitelist approach to protecting your model attributes.</description>
		<content:encoded><![CDATA[<p>With restful authentication there is a chunk of code in the user model</p>
<pre class="wp-code-highlight prettyprint">
  # HACK HACK HACK -- how to do attr_accessible from here?
  # prevents a user from submitting a crafted form that bypasses activation
  # anything else you want your user to change should be added here.
  attr_accessible :login, :email, :name, :password, :password_confirmation
</pre>
<p>Try adding :profile to the attr_accessible list or&#8230; commenting out the attr_accessible line altogether. You can use attr_protected instead and explicitly list attributes unavailable for mas assignment. A blacklist as opposed to a whitelist approach to protecting your model attributes.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

