我正在使用 Rails 3.0.10 和 ActiveAdmin 0.3.2。
我对嵌套表单和 has_one 关联有疑问。使用 has_many 我可以获得嵌套表单,但在这种情况下我确实需要理解和使用 has_one 。
模型:
class Article < ActiveRecord::Base
belongs_to :section
has_one :seo
accepts_nested_attributes_for :seo
end
class Seo < ActiveRecord::Base
belongs_to :article
end
Run Code Online (Sandbox Code Playgroud)
app/admin/article.rb怎么写?
感谢您的任何建议!
我有一个从我的一个组件传递的 ng-template,我有一个占位符来接受传递到我的组件上的 ng-template,如下面的 ngTemplateOutlet 所示。
<div>
<form novalidate #myForm="ngForm">
<ng-container>
<ng-template [ngTemplateOutlet]="myTemplate">
</ng-template>
</ng-container>
</form>
</div>
<!-- this template i am passing it from one of my other components -->
<ng-template #myTemplate>
<input type="text" name="myInput" placeholder="Input"
[(ngModel)]="inputModel" required/>
</ng-template>
Run Code Online (Sandbox Code Playgroud)
这里的问题是我的 form('myForm') 忽略了传递的 ng-template 即使它被标记为需要。我如何确保我的 ngForm 考虑传递的 ng-template
我在rails视图中有一个嵌套的表单,就像这样调用
<% f.fields_for :invoice_item_numbers do |item_no_form| %>
<%= render 'invoice_item_number', :f => item_no_form %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
部分(_invoice_item_number.html.erb)看起来像这样
<div class='invoice_item_numbers'>
<% if f.object.new_record? %>
<li><%= f.label :item_number %><%= f.text_field :item_number %>
<%= link_to_function "remove", "$(this).parent().remove()", :class => 'remove_link' %></li>
<% else %>
<li class="inline"><%= f.label :item_number %><%= f.text_field :item_number %>
</li><li class="inline"><%= f.label :description %><%= invoice_item_number.description %></li><li><%= f.label :amount %><%= f.text_field :amount %>
<%= f.check_box '_destroy', :class => 'remove_checkbox' %>
<%= f.label '_destroy', 'remove', :class => 'remove_label' %></li>
<% end …Run Code Online (Sandbox Code Playgroud) 我遇到了嵌套属性的问题.
这是我的账户模型:
class Account < ActiveRecord::Base
has_many :products
has_many :blogs
has_many :openings
has_many :users
has_one :logo, :class_name => "AccountPicture"
has_one :address, :class_name => "AccountAddress"
has_and_belongs_to_many :options
accepts_nested_attributes_for :logo, :allow_destroy => true
accepts_nested_attributes_for :address, :allow_destroy => true
accepts_nested_attributes_for :users, :allow_destroy => true
end
Run Code Online (Sandbox Code Playgroud)
这是我的用户模型:
class User < ActiveRecord::Base
belongs_to :account
end
Run Code Online (Sandbox Code Playgroud)
如您所见,Account接受徽标,地址和用户的嵌套属性.
在测试时,我可以使用嵌套属性来标识和地址,但不能用于用户.
a = Account.new
=> #<Account id: nil, hostname: nil, subdomain: nil, name: nil, description: nil, base_line: nil, footer: nil, phone_number: nil, mobile_number: nil, email_address: nil, created_at: nil, updated_at: …Run Code Online (Sandbox Code Playgroud) 一本书has_many夹克,我有一个嵌套的表格来添加它们.我想在右夹克的每组表单字段中都有一个缩略图.在书的编辑视图中获取当前夹克的最佳方法是什么?
books_controller.rb
def edit
@book = Book.find(params[:id])
end
Run Code Online (Sandbox Code Playgroud)
_jacket_fields.html.haml部分
.nested-fields
= f.inputs do
= f.input :image, :as => :file, :label => "File name"
- jacket = # what could go here?
- if jacket
= image_tag jacket.image.url(:thumb)
Run Code Online (Sandbox Code Playgroud) 我已经使用乘客和apache在linux服务器上部署了我的应用程序,我已成功完成捆绑安装运行仍然git://github.com/ryanb/nested_form.git (at master) is not checked out. Please run bundle install (Bundler::GitError)在尝试运行我的应用程序时出现()错误
仅供参考,我使用的是rails 3.2.3和ruby 1.9.3
这在我的开发机器上工作正常
任何的想法 ?
我正在使用Carrierwave将图像字段添加到基于Ryan Bates修订的#196截屏视频的Rails嵌套模型中.Carrierwave位基于他的截屏视频#253
问答模型嵌套在类别模型中.
我可以将图像上传并显示在"显示"页面上.但是当我返回编辑视图时,没有图像,我仍然看到"选择文件"按钮旁边的"未选择文件"文本.我可以前往Show页面来回播放它仍然出现在Show中,但从不出现在Edit中.我的用户会认为他们每次都要上传新图片!
我的Show视图中有这行代码:
<%= image_tag question.image_url(:thumb).to_s if question.image? %>
Run Code Online (Sandbox Code Playgroud)
但如果我尝试将其放入_form视图,那么我会得到一个未知的局部变量或方法错误.
我已经尝试了几个if语句,但还没有让它工作.我试图把它全部放在编辑视图中,但无济于事.
我的_form视图:
<%= form_for @category, :html => {:multipart => true} do |f| %>
<% if @category.errors.any? %>
<div class="error_messages">
<h2><%= pluralize(@category.errors.count, "error") %> prohibited this category from being saved:</h2>
<ul>
<% @category.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div id="categorytitle" class="field">
<%= f.label :name, "Category Name" %><br />
<%= f.text_field :name %>
</div>
<%= f.fields_for :questions do |builder| %>
<%= …Run Code Online (Sandbox Code Playgroud) 我正在使用cocoon gem处理动态嵌套的表单.我有两个型号
class CrossTable < ActiveRecord::Base
attr_accessible :title, :table_name, :database, :folder_label_id, :foreign_fields_attributes
belongs_to :folder_label
has_many :foreign_fields
accepts_nested_attributes_for :foreign_fields
validates :title, :table_name, :database, :folder_label_id, presence: true
end
class ForeignField < ActiveRecord::Base
attr_accessible :cross_table_id, :column_name, :description
belongs_to :cross_table
has_many :filter_sets
end
Run Code Online (Sandbox Code Playgroud)
我在gemfile中添加了cocoon和jquery-rails //添加// = require cocoon到application.js文件
这是我的形式部分
<%= simple_form_for @table do |f| %>
<%= f.input :title %>
<%= f.input :folder_label_id, :collection => @folders, :label_method => :title, :value_method => :id %>
<br><br>
<%= f.input :table_name %>
<%= f.input :database %>
<%= f.simple_fields_for :foreign_fields …Run Code Online (Sandbox Code Playgroud) 我有一个用户和个人资料模型,我使用相同的表单来填充它们.在我的控制器中,我有一行:@ user.build_profile
我想知道这条线做了什么.用户和配置文件之间的关系是一对一的,配置文件属于用户.
我还有一个名为image的新模型,我想使用嵌套属性与用户建立一对多的关系.在我在用户coltroller中的新动作中,我应该使用类似于上面那样的行吗?@ user.build_image
完整的新动作:
def new
@user = User.new
@user.build_profile
respond_to do |format|
format.html # new.html.erb
format.json { render json: @user }
end
end
Run Code Online (Sandbox Code Playgroud) 我对gem nested_form有一点问题。我有:
class Factura < ActiveRecord::Base
attr_accessible :itemfacturas_attributes
has_many :itemfacturas
has_many :productos, :through => :itemfacturas
accepts_nested_attributes_for :itemfacturas, :reject_if => lambda { |a| a[:descripcion].blank? }, :allow_destroy => true
Run Code Online (Sandbox Code Playgroud)
和ItemFactura类
class Itemfactura < ActiveRecord::Base
attr_accessor :vu, :vt, :descripcion
belongs_to :factura
belongs_to :producto
Run Code Online (Sandbox Code Playgroud)
我在facturas / new视图中使用了gem来添加itemfacturas。
<%= f.fields_for :itemfacturas do |b| %>
<%= render 'itemfacturas/itemfacturas', f: b %>
<% end -%>
<%= f.link_to_add "Agregar item", :itemfacturas %>
Run Code Online (Sandbox Code Playgroud)
部分是:
<%= f.number_field :cantidad, :min => 0, :value => 1 %>
<%= f.text_field :descripcion, :class => …Run Code Online (Sandbox Code Playgroud) nested-forms ×10
activeadmin ×1
angular ×1
apache ×1
carrierwave ×1
cocoon-gem ×1
database ×1
deployment ×1
forms ×1
gem ×1
ng-template ×1
production ×1
railscasts ×1
ruby ×1
simple-form ×1