Dwidoo
Dwidoo

Reputation: 77

NoMethodError in controller #new action

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

Upvotes: 1

Views: 856

Answers (1)

Kirti Thorat
Kirti Thorat

Reputation: 53038

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.

Upvotes: 4

Related Questions