未定义的方法`model_name' for NewThread :: ActiveRecord_Relation:Class

时间:2014-05-25 16:18:59

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

我读了几百个同样问题的问题,但无法弄清楚。这是我的表格:

<%= form_for(@new_thread, :url => {:action => 'create'}) do |f| %>
  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :description %><br>
    <%= f.text_field :description %>
  </div>
  <div>
    <%= f.hidden_field :user_id, :value => current_user.id %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

我的控制员:

def new
    @new_thread = NewThread.new
  end



 def create
    @new_thread = NewThread.new(new_thread_params)
    # Save the object
    if @new_thread.save
      # If save succeeds, redirect to the list action
      flash[:notice] = "Thread created."
      redirect_to(:action => 'index')
    else
      # If save fails, redisplay the form so user can fix problems
      @new_thread = NewThread.order('id ASC')
      render('new')
    end
  end

 private
    # Use callbacks to share common setup or constraints between actions.
    # Never trust parameters from the scary internet, only allow the white list through.
    def new_thread_params
      params.require(:new_thread).permit(:title, :description, :user_id)
    end

我的模特:

class NewThread < ActiveRecord::Base
    has_many :replies, :dependent => :destroy
    belongs_to :user
    has_many :like_counts, :dependent => :destroy
    has_many :dislike_counts, :dependent => :destroy
    has_many :new_thread_flags, :dependent => :destroy

    validates_presence_of :title
    validates_presence_of :description
end

与new_thread关联的路线:

                         GET    /new_threads(.:format)                                 new_threads#index
                         POST   /new_threads(.:format)                                 new_threads#create
          new_new_thread GET    /new_threads/new(.:format)                             new_threads#new
         edit_new_thread GET    /new_threads/:id/edit(.:format)                        new_threads#edit
              new_thread GET    /new_threads/:id(.:format)                             new_threads#show
                         PATCH  /new_threads/:id(.:format)                             new_threads#update
                         PUT    /new_threads/:id(.:format)                             new_threads#update
                         DELETE /new_threads/:id(.:format)                             new_threads#destroy
                         DELETE /reply_flags/:id(.:format)                             reply_flags#destroy
      new_threads_search POST   /new_threads/search(.:format)                          new_threads#search
                    root GET    /                                                      new_threads#list

一切看起来都很好。当我提交我的表格与空字段时,我得到这个:

undefined method `model_name' for NewThread::ActiveRecord_Relation:Class

否则它工作得很好。我使用相同的形式部分更新,验证也有效。我正在使用rails 4。

2 个答案:

答案 0 :(得分:0)

问题在于此处,在保存失败后初始化新的@new_thread

@new_thread = NewThread.order('id ASC')

你期待在那里发生什么?也许你的意思是这个?

@new_thread = NewThread.order('id ASC').first

如果不选择NewThread一个,视图将尝试渲染它们的整个集合

答案 1 :(得分:0)

你应该删除这一行:

@new_thread = NewThread.order('id ASC')

否则当保存失败时,您将为用户提供不同的模型进行编辑。您已经分配了@new_thread,所以一切都会有效。