Eurekamoments中的NoMethodError#new | #Eurekamoment的未定义方法`field':0x52c6f80> </eurekamoment:0x52c6f80>

时间:2014-04-05 14:55:51

标签: ruby-on-rails methods ruby-on-rails-4

我收到以下错误:

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

在这一行:

 <%= f.text_field :field %>

从这个视图

  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 %>

这是我的控制者:

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

这是我的模特:

class Eurekamoment < ActiveRecord::Base

    has_many :comments
    belongs_to :user
    has_many :votes

end

我的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

我在最后一小时浪费时间试图在黑客马拉松上解决这个问题而无法解决这个问题,你们有没有看错?感谢。

1 个答案:

答案 0 :(得分:0)

您的代码看起来很好 - 错误几乎肯定会成为数据库问题,正如您的评论中所述

我认为这将是你问题的真正原因:I've wasted last hour trying to figure this out at a hackathon and can't fix this issue - 最好退后一步,深呼吸并按逻辑处理

我将从总数据库刷新开始。放下桌子和桌子然后再次运行rake db:migrate,或者您可以使用rake db:migrate VERSION=0rake db:migrate:reset - How can I migrate my database with rails to the first revision without dropping the database first?