Ruby on rails 未定义的方法'to#u key';对于#<;ActiveRecord::关系[]>;

Ruby on rails 未定义的方法'to#u key';对于#<;ActiveRecord::关系[]>;,ruby-on-rails,paperclip,Ruby On Rails,Paperclip,我正试图用回形针上传一个新文件 def new @other_font = OtherFont.all render :"other_fonts/new" end def create @new_font = OtherFont.new(font_params) if @new_font.save redirect other_fonts_path else flash[:notice] = "Wa

我正试图用回形针上传一个新文件

def new
        @other_font = OtherFont.all
        render :"other_fonts/new"
    end

def create
    @new_font = OtherFont.new(font_params)
    if @new_font.save
        redirect other_fonts_path
    else
        flash[:notice] = "Was not able to upload, try again"
        render :'other_fonts/new'
    end
end
这里是风景

<div id="other fonts">
 <%=form_for @other_font,:html => {:multipart => true} do |f| %>
 <div class="col-2">
  <label>
    <%=f.file_field :file %>
  </label>
   </div>

 <%=f.submit "Upload Fonts" %> 
<% end %> 
</div>

{:multipart=>true}do | f |%>
我得到了这个错误

NoMethodError - undefined method `to_key' for #<ActiveRecord::Relation []>: 
NoMethodError-未定义的方法“to#u key”用于#:

我用过回形针,已经有一段时间了。我在谷歌上搜索了错误,人们似乎都有相同的代码。

你需要创建一个新对象。因此,你的新行动应该是:

def new
    @other_font = OtherFont.new
    render :"other_fonts/new"
end
def new
    @other_font = OtherFont.new
end
您的表单需要一个新对象。但是你给了他一个ActiveRecord::关系

你的控制器叫什么名字? -如果它是
OtherFontsController
,则不需要调用
render:“other\u font/new”
,您的操作应该是:

def new
    @other_font = OtherFont.new
    render :"other_fonts/new"
end
def new
    @other_font = OtherFont.new
end

你能给出完整的堆栈跟踪吗?谢谢,当我换成新的时,这个问题已经解决了@请将我的答案标记为解决方案;)