我試圖創建一個單一的形式允許創建評論和相關的附件Rails的accepts_nested_attributes給未定義的方法
評論型號有:
class Comment < ActiveRecord::Base
has_many :attachments
accepts_nested_attributes_for :attachments
end
評論控制器有:
# GET /comments/new
# GET /comments/new.json
def new
@comment = Comment.new
@worequest = params[:worequest_id]
respond_to do |format|
format.html # new.html.erb
format.json { render json: @comment }
end
end
在意見表,我想補充一點:
<%= simple_form_for @comment, :html => {:class => 'form-horizontal'} do |f| %>
(CODE FOR COMMENT)
<% f.fields_for @attachments do |builder| %>
<%= builder.input :name, :label => 'Attachment Name' %>
<%= builder.file_field :attach, :label => 'Attachment File' %>
<% end %>
但是,我得到這個錯誤:
undefined method `model_name' for NilClass:Class
感謝您的幫助!
您在'simple_form_for'中使用的'@ comment'對象是'nil'。請發佈您的控制器代碼。 – vee
您未定義@attachments – Donovan