0

I am getting the following error:

NoMethodError in Eurekamoments#new
undefined method `field' for #<Eurekamoment:0x52c6f80>

on this line:

 <%= f.text_field :field %>

from this view

  This is new view

<%= form_for(@eurekamoment) do |f| %>
  <% if @eurekamoment.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@eurekamoment.errors.count, "error") %> prohibited this link from being saved:</h2>

      <ul>
      <% @eurekamoment.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :eurekamoment %><br />
    <%= f.text_field :eurekamoment %>
  </div>
  <div class="field">
    <%= f.label :field %><br />
    <%= f.text_field :field %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

This is my controller:

class EurekamomentsController < ApplicationController

    def index
      @eurekamoment = Eurekamoment.paginate(:page => params[:page], :per_page => 3 )
      params[:per_page] ||= 25
      params[:page]     ||= 1
    end

    def show
      @eurekamoment   = Eurekamoment.find(params[:id])
      @comment = Comment.new
      @vote = Vote.new
    end

    def new
      @eurekamoment = Eurekamoment.new
    end

    def create
      @eurekamoment = Eurekamoment.new(link_params)
        if @eurekamoment.save
            redirect_to @eurekamoment 
        else
            render action: 'new'
        end
    end

    def destroy
      @eurekamoment = Eurekamoment.find(params[:id])
      if @eurekamoment.destroy
        redirect_to action: 'index'
      else
        render action: 'show'
      end
    end

    def edit
      @eurekamoment = Eurekamoment.find(params[:id])
    end

    def update
    @eurekamoment = Eurekamoment.find params[:id]
      if @eurekamoment.update(eurekamoment_params)
        redirect_to @eurekamoment 
      else
        render action: 'edit'
      end
    end

private
    def eurekamoment_params
      params.require(:eurekamoment).permit(:id, :eurekamoment, :field, :description, :plan)
    end


end

This is my model:

class Eurekamoment < ActiveRecord::Base

    has_many :comments
    belongs_to :user
    has_many :votes

end

And my db:

class CreateEurekamoments < ActiveRecord::Migration
  def change
    create_table :eurekamoments do |t|

            t.integer :user_id
            t.string  :field
            t.string  :eurekamoment
            t.string  :description
            t.string  :plan

      t.timestamps
    end
  end
end

I've wasted last hour trying to figure this out at a hackathon and can't fix this issue, do you guys see anything wrong? Thanks.

user3408293
  • 1,377
  • 6
  • 18
  • 26
  • 1
    Did you run rake db:migrate? Does the field "field" exist in your DB? – gernberg Apr 05 '14 at 14:57
  • Nothing happens when I run rake db:migrate its weird. I have a field in my db but not sure it got added? It seems my rake db:migrate is perhaps not working – user3408293 Apr 05 '14 at 15:03
  • I ran a rake db:reset just before this and I think that might ave messed it up. What can I do to get my migrations working again.? – user3408293 Apr 05 '14 at 15:05
  • saw in my schema that my fields table wasn't forming so I forced the migration to re-run and then it made the table but I still get the same error message... – user3408293 Apr 05 '14 at 15:16

1 Answers1

0

Your code looks good - the error will almost certainly be a DB issue, as discussed in your comments

I think this will be the real cause of your problem: I've wasted last hour trying to figure this out at a hackathon and can't fix this issue -- it's best to take a step back, deep breath and work through it logically

I'd start with a total db refresh. Drop your tables & then run rake db:migrate again, or you could use rake db:migrate VERSION=0, or rake db:migrate:reset - How can I migrate my database with rails to the first revision without dropping the database first?

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147