我需要验证存储在表单bean对象列表中的对象.
下面是我的表单bean对象.
public class Role implements java.io.Serializable {
// Fields
private int roleId;
@NotBlank
private String roleName;
private boolean active;
@Valid
private List<Module> modules;
// getters anfd setters
}
Run Code Online (Sandbox Code Playgroud)
以下是我的主要表单bean对象列表中的对象
public class Module implements Serializable {
private int id;
@NotBlank
private String moduleName;
// other properties and getters and setters
}
Run Code Online (Sandbox Code Playgroud)
下面是我的属性文件
# -- Role form --
NotBlank.role.roleName=Role Name can not be blank.
NotBlank.module.moduleName=Module Name can not be blank.
Run Code Online (Sandbox Code Playgroud)
下面是My JSP页面,该表单由角色名称和可添加到角色的模块组成.
<table border="0" class="section_tbl2">
<tr>
<td width="150px" valign="top">
<spring:message code="dmx.role.form.label.name"/> …Run Code Online (Sandbox Code Playgroud) 我严重关注我有用户和个人资料模型的问题.无论我尝试什么,似乎都没有触发任何Profile属性的验证
我的表格:
- title t(:title, :scope => :register)
%h1= yield(:title)
= simple_form_for(resource, :as => resource_name, :html => { :class => 'form-horizontal' } , :validate => true , :url => registration_path(resource_name)) do |f|
= f.input :username, :label => t(:username)
= f.input :email, :label => t(:email),
:hint => t(:hint_email_visible)
= f.input :password, :label => t(:password), :require => true
= f.input :password_confirmation, :label => t(:password_confirm)
- resource.build_profile
= f.fields_for :profile do |f|
#div
= f.hidden_field :form, :value => …Run Code Online (Sandbox Code Playgroud) validation activerecord ruby-on-rails nested-attributes ruby-on-rails-3
ActiveRecord似乎并不理解,给定具有嵌套属性的现有记录的一组参数,它可以创建一个新的嵌套记录,该记录本身具有嵌套的现有记录.(关系树:Existing -> New -> Existing)
这是一个错误,还是我错过了什么?
让我举个简单的例子.
这是我的模特:
class User < ActiveRecord::Base
has_many :posts
attr_accessible :name, :posts_attributes
accepts_nested_attributes_for :posts
end
class Post < ActiveRecord::Base
belongs_to :group
belongs_to :user
attr_accessible :content, :title, :group_attributes
accepts_nested_attributes_for :group
end
class Group < ActiveRecord::Base
has_many :posts
attr_accessible :name
end
Run Code Online (Sandbox Code Playgroud)
我在每个表中都创建了一条记录,并相应地将它们相关联,因此每个表中都有一条记录 - 这id=1是已知的.现在,如果我有一个现有用户,一个新帖子和一个现有组,并尝试使用accepts_nested_attributes_for它更新该记录,它不喜欢它:
1.9.3-p125 :044 > params
{
:id => 1,
:name => "Billy",
:posts_attributes => [
[0] {
:title => "Title",
:content => "Some magnificent content for …Run Code Online (Sandbox Code Playgroud) 我有一个发票模型,"有很多"发票项目.我有一个表单,允许您创建/编辑发票,此表单接受发票项目的嵌套属性.
在我的发票模型中,有一个"total_amount"字段,它是一个计算字段(发票项目中"金额"的总和).
我想在创建或更新发票时计算此金额.我试图在发票模型的before_save事件中执行此操作.下面的代码几乎可以工作,但是保存的总数总是落后一步.即如果我的发票总额为20美元,我编辑此发票并将发票项目更改为总计15美元,然后保存我的发票,总数不会更改.如果我打开相同的发票,然后再次保存,则总计会正确更新.
我假设下面计算总和的行是访问已经保存在数据库中的行项目,而不是那些刚刚更改过并且即将保存的行项目.但我不知道如何访问它们.
class Invoice < ActiveRecord::Base
has_many :invoice_items, :dependent => :destroy
accepts_nested_attributes_for :invoice_items, :allow_destroy => true
before_save :record_total_amount
private
def record_total_amount
self.total_amount = self.invoice_items.sum('amount')
end
end
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激.
我在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) 正如预期的那样,我的部分渲染两次而不是一次.有什么想法吗?
这是我的人物观点
<%= simple_nested_form_for(@person) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name %>
<h3>Records by year</h3>
<div id='records'>
<%= f.simple_fields_for :records do |record| %>
<%= render 'record_fields', :f => record %>
<% end %>
<div class='links'>
<%= link_to_add_association 'New Record', f, :records, :class => 'btn' %>
</div>
</div>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)
模型(删除常量和验证等内容):
class Person < ActiveRecord::Base
attr_accessible :name, :records_attributes
has_many :records, :dependent => :destroy
accepts_nested_attributes_for :records, :reject_if => :all_blank, …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) 我无法弄清楚如何在Active Admin中使用嵌套资源输入助手,以允许我更新"父"记录的相关记录的值.
我正在尝试生成更新的模型是这样的:
class Page < ActiveRecord::Base
has_many :page_attributes
accepts_nested_attributes_for :page_attributes, allow_destroy: true
end
Run Code Online (Sandbox Code Playgroud)
哪里PageAttribute有两个属性,:key和:value
而ActiveAdmin模型是:
ActiveAdmin.register Page do
permit_params page_attributes_attributes: [:key, :value, :_destroy => true]
form do |f|
f.inputs do
f.has_many :page_attributes, allow_destroy: true, heading: 'Parts' do |page_part|
page_part.input :key
page_part.input :value
end
end
f.actions
end
end
Run Code Online (Sandbox Code Playgroud)
但是当我调用http://localhost:3000/admin/pages/2/edit并更改现有属性的值时(或者当我检查"删除"复选框时),会发生什么,PageAttribute即创建模型的新记录并保持现有关联不变.
我通读了有关嵌套资源的Active Admin文档,以及一堆SO帖子,但无法弄清楚我做错了什么:(
我有一个Bar属于的嵌套资源Foo.我可以成功列出Bar属于任何给定的所有对象Foo.但我也希望能够生成一个视图,其中包含所有Bar项目,无论Foo它们属于哪个对象.
模型结构是:
# app/models/foo.rb
class Foo < ActiveRecord
has_many :bars
end
# app/models/bar.rb
class Bar < ActiveRecord
belongs_to :foo
end
Run Code Online (Sandbox Code Playgroud)
路由定义为:
# config/routes.rb
resources :foos do
resources :bars
end
Run Code Online (Sandbox Code Playgroud)
我从这个配置中获得了预期的路由:
foo_bars GET /foos/:foo_id/bars(.:format) bars#index
POST /foos/:foo_id/bars(.:format) bars#create
new_foo_bar GET /foos/:foo_id/bars/new(.:format) bars#new
edit_bar GET /bars/:id/edit(.:format) bars#edit
bar GET /bars/:id(.:format) bars#show
PATCH /bars/:id(.:format) bars#update
PUT /bars/:id(.:format) bars#update
DELETE /bars/:id(.:format) bars#destroy
foos GET /foos(.:format) foos#index
POST /foos(.:format) foos#create
new_foo GET /foos/new(.:format) …Run Code Online (Sandbox Code Playgroud) 我有一个Profile模特,那个accepts_nested_attributes_for :grades.
我的Grade模型看起来像这样:
# == Schema Information
#
# Table name: grades
#
# id :integer not null, primary key
# subject :string
# result :string
# grade_type :integer
# profile_id :integer
# created_at :datetime not null
# updated_at :datetime not null
class Grade < ActiveRecord::Base
belongs_to :profile
enum grade_type: { csec: 0, cape: 1, sat: 2, g7: 3, g8: 4, g9: 5, g10: 6, g11: 7, g12: 8, g13: 9 }
end
Run Code Online (Sandbox Code Playgroud)
在我 …
activerecord ×2
activeadmin ×1
cocoon-gem ×1
has-many ×1
nested-forms ×1
simple-form ×1
spring-mvc ×1
validation ×1