Question

I know that I'm a newbie at this and that I am not worthy, but could someone please explain to me why I am getting a No Method Error? Here's what I did. I generated a new migration for my database onto my existing rails app, and the migration is called "Profile". I ran db:migrate and then proceeded to edit my previous "new.html.erb" form. The code looks like this:

class CreateProfiles < ActiveRecord::Migration
  def self.up
    create_table :profiles do |t|
      t.string :major
      t.string :year
      t.string :books_sell
      t.string :books_buy
      t.string :facebook
      t.string :restaurants
      t.string :interests

      t.timestamps
    end
    add_index :profiles, :major
    add_index :profiles, :year
    add_index :profiles, :books_sell
    add_index :profiles, :books_buy
    add_index :profiles, :facebook
    add_index :profiles, :restaurants
    add_index :profiles, :interests
  end

  def self.down
    drop_table :profiles
  end
end

Basically, I am adding a profile section onto my app, but I am getting this:

   undefined method `major' for #<User:0x00000100b6e030>
    Extracted source (around line #23):

    20:   </div>
    21:   <div class="field">
    22:     <%= f.label :"major" %><br />
    23:     <%= f.text_field :major %>
    24:   </div>

This is my views/users/new.hmtl.erb file:

<h1>Sign up</h1>

<%= form_for(@user) do |f| %>
  <%= render 'shared/error_messages', :object => f.object %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :password %><br />
    <%= f.password_field :password %>
  </div>
  <div class="field">
    <%= f.label :password_confirmation, "Confirmation" %><br />
    <%= f.password_field :password_confirmation %> 
  </div>
  <div class="field">
    <%= f.label :"year" %><br />
    <%= f.text_field :year %>
  </div>
  <div class="field">
    <%= f.label :"major" %><br />
    <%= f.text_field :major %>
  </div>
  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>

What's missing?

Was it helpful?

Solution

The problem here was that I previously had a form view completed under the user model. I wanted to tack onto that form and so I created a new migration named profile. I did this because I was not able to manually rollback my migration of the user model and just tack on strings and columns.

However, adding text fields from the profile model under the user model poses an error.

What I did instead was I created a Add_xxx_to_yyy migration which allowed me to add columns onto a previously created migration without any problems. I used rails generate migration Add_profile_to_User with the underscores because I'm on rails 3.0 (it didn't work when I did Addprofiletouser). Et voilà!

OTHER TIPS

You've pasted your migration for Profile model. And I guess, that in your @user variable you have new instance of User model.

And since there is no method or attribute major defined for your User, you see the complaint "undefined method..."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top