1

The solution might be obvious but I have looked at a lot of posts on Stackoverflow and couldn't solve this error.

app/views/sellers/_form.html.erb where line #16 raised:
undefined method `name' for #<Seller:0x007fbedb5f3ce0>

    Extracted source (around line #16):   

 <div class="field">
   <%= f.label :name %><br>
   <%= f.text_field :name %>
 </div>

beginning of _form.html.erb

<%= form_for(@seller) do |f| %>
  <% if @seller.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@seller.errors.count, "error") %> prohibited this seller from being saved:</h2>
      <ul>
        <% @seller.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>

sellers_controller (edit : here is the controller asked below)

class SellersController < ApplicationController
  def new
    @seller = Seller.new
  end
end

schema.rb

create_table "sellers", force: true do |t|
  t.string   "name"
  t.string   "person_type"
  t.string   "rep_first_name"
  t.string   "rep_last_name"
  t.date     "rep_birthday"
  t.string   "rep_nationality"
  t.string   "rep_country_of_residence"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.integer  "mp_id"
  t.integer  "mp_walletid"
end

seller.rb

class Seller < User
  has_many :resas
  has_many :buyers, :through => :resas
  validates :name, :person_type, :rep_first_name, :rep_last_name, :rep_birthday, :rep_nationality, :rep_country_of_residence, presence: true
  validates :name, length: { maximum: 255 }
  validates :rep_first_name, :rep_last_name, length: { maximum: 100 }
  validates :rep_nationality, :rep_country_of_residence, length: { is: 2 }
  def seller?
    self.is_a?(Seller)
  end
end

user.rb

class User < ActiveRecord::Base
  has_one :seller, dependent: :destroy
  accepts_nested_attributes_for :seller
  has_one :buyer, dependent: :destroy
  accepts_nested_attributes_for :buyer
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
  def type
    self.type
  end
end
Dwidoo
  • 77
  • 10

1 Answers1

4

The error :

undefined method 'name' for #<Seller:0x007fbedb5f3ce0>

will occur when you don't have attribute name defined for Seller model. To resolve this issue, either add name as a field to sellers table or make it as a virtual attribute in Seller model depending upon your requirement.

UPDATE

Looks like you are trying to implement Single Table Inheritance(STI).

If that is the case, then you should be having just a single table i.e., users with a field named type which is used for identification of the child like Seller in your case.

I would highly recommend reading @tadman's answer Single Table Inheritance to refer to a child class with its own fields. It will definitely help you to setup proper STI.

Community
  • 1
  • 1
Kirti Thorat
  • 52,578
  • 9
  • 101
  • 108
  • Share the `schema.rb` file in the question, so we can verify that `name` field does exist in `sellers` table. – Kirti Thorat Jul 21 '14 at 21:44
  • 1
    That's strange. Then, I doubt that there is some problem in the model. Can you share the model `Seller` in the question. – Kirti Thorat Jul 21 '14 at 21:52
  • Thanks. Could you also share the User model as Seller inherits it. – Kirti Thorat Jul 21 '14 at 22:13
  • Just shared. An other thought that could help, sooner, Devise was added to Seller, but I removed it (cleanly I thought, through down-migration and rails devise destroy). Then, I added Devise to User. – Dwidoo Jul 21 '14 at 22:20
  • Thank you. I did implement STI with a field `type`in User. I've read the page you recommend but I don't understand in what way this helps me here. This seem very complex. User has `email`, `password` required fields, but Seller has required fields which are different from Buyer's required fields (Buyer also inherits from User). My implementation is based on @mohamagdy's answer [link](http://stackoverflow.com/questions/9472852/devise-and-multiple-user-models) – Dwidoo Jul 21 '14 at 22:49
  • If you notice in the linked answer, tadman has shown how STI should be implemented and also how the additional fields if required, should be added to a new model (refer admin_profiles and common_profiles example in his answer) – Kirti Thorat Jul 21 '14 at 22:55