我有一个模型Note
和它的属性是subject
和content
问题是,创建一个新的笔记时,内容变得nil
。模型内容没有得到保存
型号/ notes.rb
belongs_to :user
scope :english, lambda { where("note.subject = ?", "english") }
scope :math, lambda { where("note.subject = ?", "math") }
控制器/ notes.rb
def new
@note = current_user.notes.new
respond_with(@note)
end
def create
@note = current_user.notes.new(note_params)
@note.save
redirect_to user_note_path(current_user.id, @note)
end
private
def set_note
@note = current_user.notes.find(params[:id])
end
def note_params
params.permit(:content, :subject, :user_id)
end
参数提交后/笔记/新形式
Parameters: {.."note"=>{"content"=>"new note"}, "subject"=>"english", "commit"=>"Submit", "user_id"=>"1"}
笔记/ _form.html。 erb
<%= form_for([current_user, @note]) do |f| %>
<%= f.label :content %>
<%= f.text_area :content %>
<%= f.label :subject %>
<%= select_tag(:subject, options_for_select(["english", "math"])) %>
<%= f.submit "Submit"%>
<% end %>
当你说“的内容成为无“内容属性保存无或所有属性保存为零? – Pavan
内容属性保存为零 –
请张贴您的表单代码 – Pavan