在我的应用程序中,用户有很多项目.我想创建一个" 添加多个项目 "表单,因此用户可以一次创建多个项目.
在我看来,最快的方法是在其中嵌套项目字段的用户表单,并省略用户字段.这样,当提交表单时,将保存用户并自动创建所有新的项目记录.
但是,我不希望现有的项目显示在表单中.只有正在创建的新项目的空字段(来自@ user.projects.build).是否有我可以传递的参数或我可以在表单中更改的内容以省略现有的Project记录?
<% form_for (@user) do |f| %>
<% f.fields_for :project do |project_form| %>
<%= render :partial => 'project', :locals => {:f => project_form} %>
<% end %>
<%= add_child_link "New Project", f, :projects %>
<%= f.submit "save" %>
<%end%>
Run Code Online (Sandbox Code Playgroud)
我正在使用Ryan Bate的复杂形式示例.代码工作正常.我只是想省略现有项目以这种形式出现.
我有以下课程:
在Project模型中,我添加了以下语句:
has_and_belongs_to_many :people
accepts_nested_attributes_for :people
Run Code Online (Sandbox Code Playgroud)
当然还有课堂上适当的陈述Person.如何通过方法添加Developer到a ?以下不起作用:Projectnested_attributes
@p.people_attributes = [{:name => "Epic Beard Man", :type => "Developer"}]
@p.people
=> [#<Person id: nil, name: "Epic Beard Man", type: nil>]
Run Code Online (Sandbox Code Playgroud)
如您所见,type属性设置为nil而不是"Developer".
inheritance ruby-on-rails single-table-inheritance nested-attributes
我在show.erb中嵌套了属性,并创建了一个空白的嵌套属性,并显示了底部空白的项目网格,如此.
<%= form_for @question do |q| %>
<% q.fields_for :answers, @question.answers do |l| %>
<tr>
<td><%= l.text_field :text %></td>
<td><%= l.check_box :correct %></td>
<td><%= l.text_field :imagename %></td>
<td><%= l.number_field :x %></td>
<td><%= l.number_field :y %></td>
</tr>
<% end %>
<tr>
<td colspan=5 align=right><%= submit_tag '+' %>
</tr>
<% end %>
Run Code Online (Sandbox Code Playgroud)
我想要一个link_to'Destroy'工作,但是undefined method 'plural'当我将它添加到网格时我得到了
<%= link_to 'Destroy', l, :controller => "answer", :confirm => 'Are you sure?', :method => :delete %>
Run Code Online (Sandbox Code Playgroud) 我有这个一对多的关系:
class Programa < ActiveRecord::Base
attr_accessible :descripcion, :nombre, :roles_attributes
has_many :roles, :dependent => :restrict
accepts_nested_attributes_for :roles
...
end
class Role < ActiveRecord::Base
attr_accessible :description, :name, :programa_id
belongs_to :programa
...
end
Run Code Online (Sandbox Code Playgroud)
它适用于rails控制台:
> params = { programa: { nombre: 'nuevo', roles_attributes: [ {name: 'role1'}, {name: 'role2'}] }}
> p = Programa.create(params[:programa])
> p
=> #<Programa id: 7, nombre: "nuevo", descripcion: nil, created_at: "2013-10-09 14:07:46", updated_at: "2013-10-09 14:07:46">
> p.roles
=> [#<Role id: 15, name: "role1", description: nil, created_at: "2013-10-09 14:07:46", updated_at: …Run Code Online (Sandbox Code Playgroud) 我很难搞清楚如何在rails中序列化模型的嵌套属性.我有一个RecipeTemplate,它将已存在的Recipe存储在它的template_data属性中.Recipe有两个级别的嵌套属性.
这是在rails 3.1.0.rc4上
class RecipeTemplate < ActiveRecord::Base
serialize :template_data, Recipe
...
end
class Recipe < ActiveRecord::Base
has_many :ingredients
accepts_nested_attributes_for :ingredients
...
end
Run Code Online (Sandbox Code Playgroud)
Recipe中的成分也有嵌套属性(SubIngredients).
如果我使用如下对象设置template_data:
Recipe.includes(:ingredients => [:sub_ingredients]).find(1)
Run Code Online (Sandbox Code Playgroud)
我会得到一个TypeError"无法转储匿名类Class",这是有道理的,因为它不知道如何序列化Ingredients或SubIngredients.
如何序列化模型中的嵌套属性,以便您可以使用:
serialize :template_data, Recipe
Run Code Online (Sandbox Code Playgroud)
或者我是否必须以其他方式序列化数据并自行执行类型安全检查?
在此先感谢您的帮助
当我发布表单以创建带有子评论的新查询时(在应用程序中,查询可以有多个评论),评论不会被构建.它在删除状态验证时有效.所以它与构建和保存事物的顺序有关.如何保留验证并保持代码清洁?
(以下是一个示例,因此可能无法完全运行)
车型/ inquiry.rb
class Inquiry < ActiveRecord::Base
has_many :comments
accepts_nested_attributes_for :comments
Run Code Online (Sandbox Code Playgroud)
车型/ comment.rb
class Comment < ActiveRecord::Base
belongs_to :inquiry
belongs_to :user
validates_presence_of :user_id, :inquiry_id
Run Code Online (Sandbox Code Playgroud)
控制器/ inquiry_controller.rb
expose(:inquiries)
expose(:inquiry)
def new
inquiry.comments.build :user => current_user
end
def create
# inquiry.save => false
# inquiry.valid? => false
# inquiry.errors => {:"comments.inquiry_id"=>["can't be blank"]}
end
Run Code Online (Sandbox Code Playgroud)
意见/查询/ new.html.haml
= simple_form_for inquiry do |f|
= f.simple_fields_for :comments do |c|
= c.hidden_field :user_id
= c.input :body, :label => 'Comment'
= f.button :submit
Run Code Online (Sandbox Code Playgroud)
数据库架构
create_table "inquiries", …Run Code Online (Sandbox Code Playgroud) 现在,我有一个服务器调用踢回以下Ruby哈希:
{
"id"=>"-ct",
"factualId"=>"",
"outOfBusiness"=>false,
"publishedAt"=>"2012-03-09 11:02:01",
"general"=>{
"name"=>"A Cote",
"timeZone"=>"EST",
"desc"=>"À Côté is a small-plates restaurant in Oakland's charming
Rockridge district. Cozy tables surround large communal tables in both
the main dining room and on the sunny patio to create a festive atmosphere.
Small plates reflecting the best of seasonal Mediterranean cuisine are served
family-style by a friendly and knowledgeable staff.\nMenu items are paired with
a carefully chosen selection of over 40 wines by the glass as well as …Run Code Online (Sandbox Code Playgroud) 我的问题是我遇到了accepts_nested_attributes_for的限制,所以我需要弄清楚如何自己复制该功能以获得更大的灵活性.(请参阅下文,了解究竟是什么让我失望.)所以我的问题是:如果我想模仿并增加accepts_nested_attributes_for,我的表单,控制器和模型应该是什么样的?真正的诀窍是我需要能够使用现有的关联/属性更新现有的AND新模型.
我正在构建一个使用嵌套表单的应用程序.我最初使用这个RailsCast作为蓝图(利用accepts_nested_attributes_for):Railscast 196:嵌套模型表单.
我的应用程序是带有作业(任务)的清单,我让用户更新清单(名称,描述)并在单个表单中添加/删除关联的作业.这很好用,但是当我将其合并到我的应用程序的另一个方面时,我遇到了问题:历史记录通过版本控制.
我的应用程序的一个重要部分是我需要记录我的模型和关联的历史信息.我最终推出了自己的版本(这是我在描述我的决策过程/注意事项的问题),其中很大一部分是我需要创建旧版本的新版本的工作流程,对新版本进行更新,归档旧版本.这对于用户是不可见的,用户将该体验视为仅通过UI更新模型.
代码 - 模型
#checklist.rb
class Checklist < ActiveRecord::Base
has_many :jobs, :through => :checklists_jobs
accepts_nested_attributes_for :jobs, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
end
#job.rb
class Job < ActiveRecord::Base
has_many :checklists, :through => :checklists_jobs
end
Run Code Online (Sandbox Code Playgroud)
代码 - 当前表单(注意:@jobs在检查清单控制器编辑操作中被定义为此清单的未归档作业;因此是@checklist)
<%= simple_form_for @checklist, :html => { :class => 'form-inline' } do |f| %>
<fieldset>
<legend><%= controller.action_name.capitalize %> Checklist</legend><br>
<%= f.input :name, :input_html => { :rows => 1 }, :placeholder => …Run Code Online (Sandbox Code Playgroud) ruby-on-rails nested-forms nested-attributes model-associations update-attributes
我的问题有点类似于问题nested_form gem add works但删除失败...为什么?.
我有一个产品编辑页面,其中产品的子类别在product_sub_categories中链接.要将子类别分配给产品,我使用了product_sub_categories的嵌套属性.因此,产品可以有多个sub_categories.
在产品型号中,
has_many :product_sub_categories
has_many :sub_categories, :through => :product_sub_categories
accepts_nested_attributes_for :product_sub_categories, :allow_destroy => true
Run Code Online (Sandbox Code Playgroud)
并在产品编辑视图中:
<%= f.fields_for :product_sub_categories do |product_sub_category| %>
<%= product_sub_category.collection_select :sub_category_id, @sub_categories, :id, :sub_category, {:include_blank => 'Select a Sub Category'} %>
<%= product_sub_category.link_to_remove "Remove", :class => "subcatlink" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
代码适用于添加子类别.但是当我删除子类别时失败了.日志给出:
"product_sub_categories_attributes"=>{"0"=>{"sub_category_id"=>"1", "_destroy"=>"false", "id"=>"9"}, "1"=>{"sub_category_id"=>"1", "_destroy"=>"1", "id"=>"17"}},
ProductSubCategory Load (0.2ms)[0m [1mSELECT `product_sub_categories`.* FROM `product_sub_categories` WHERE `product_sub_categories`.`product_id` = 8 AND `product_sub_categories`.`id` IN (9, 17)
Run Code Online (Sandbox Code Playgroud)
虽然,我点击删除,它只是传递_destroy ="1",但不会破坏子类别.
任何人都可以告诉解决方案吗?
更新:
非常抱歉我的愚蠢错误.我没有看到正确的代码.在我复制的模型中
accepts_nested_attributes_for :product_sub_categories …Run Code Online (Sandbox Code Playgroud) nested-forms nested-attributes ruby-on-rails-3 ruby-on-rails-3.2
我正在尝试自定义用户在错误输入数据时在窗体顶部看到的错误消息警报.我正在尝试自定义的错误消息提醒是针对嵌套形式的模型属性.
我在这里尝试过编写文件的解决方案,config/locales/en.yml但这只会更改消息而不是错误消息之前显示的模型和属性的名称.
我也尝试过Billy在他的回答中提出的建议,结果相同.即
1个错误禁止保存这个徒步旅行车:
- 来自"我的自定义空白错误消息"的路线指示
有没有办法让我在错误消息中显示更加用户友好的模型和属性名称,或者从错误消息中完全删除它们?
这是我有的:
# Sample localization file for English. Add more files in this directory for other locales.
# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
en:
activerecord:
models:
direction: "In the Getting There section"
attributes:
direction:
directions_from: "From field"
errors:
full_messages:
format: "%{message}"
models:
direction:
attributes:
directions_from:
blank: "My Custom Blank Error Message"
Run Code Online (Sandbox Code Playgroud)
class Direction < ActiveRecord::Base
belongs_to :hikingtrail
attr_accessible :directions_by, :directions_desc, :directions_from
validates :directions_from, …Run Code Online (Sandbox Code Playgroud) nested-forms ×4
activerecord ×1
alert ×1
has-many ×1
hash ×1
inheritance ×1
locale ×1
ruby ×1
simple-form ×1