Ruby on rails 3 如何在不使用表单的情况下填充数据库/模型?
如何在不使用表单的情况下填充模型?如果以下模型为示例模型:Ruby on rails 3 如何在不使用表单的情况下填充数据库/模型?,ruby-on-rails-3,model,Ruby On Rails 3,Model,如何在不使用表单的情况下填充模型?如果以下模型为示例模型: class Project < ActiveRecord::Base attr_accessible :project_name belongs_to :user validates :project_name, :presence => true validates :user_id, :presence => true end class项目true 验证:use
class Project < ActiveRecord::Base
attr_accessible :project_name
belongs_to :user
validates :project_name, :presence => true
validates :user_id, :presence => true
end
class项目true
验证:user_id,:presence=>true
结束
通常会使用以下形式:
<%= form_for @project do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.text_field :project_name %>
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>
f、 对象%>
但是,如果我想在不使用表单的情况下填充模型(比如,如果我想在ruby中进行一些内部计算/操作,并使用输出填充表单),我该怎么做呢?您只需创建模型的一个新对象并通过以下方式保存它:entry.save填充模型的最佳方法是使用种子。 或者,您也可以根据需要执行一些rake任务