我有
class Profile
has_many :favorite_books, :dependent => :destroy
has_many :favorite_quotes, :dependent => :destroy
accepts_nested_attributes_for :favorite_books, :allow_destroy => true
accepts_nested_attributes_for :favorite_quotes, :allow_destroy => true
end
Run Code Online (Sandbox Code Playgroud)
我有一个动态表单,你按"+"添加新的textareas来创建新的收藏夹.我想要做的是忽略空白,我发现在更新控制器中比非嵌套属性更难排序.
我暂时拥有的是删除空记录的after_save回调中的黑客攻击.什么是最容易忽略这些空白对象的轨道方式?
我不想要验证和错误,只是一个无声的删除/忽略.
我试图创建两个隐藏字段,一个显示没有问题,但另一个来自嵌套表单不会
product.rb
class Product < ActiveRecord::Base
has_many :product_options, dependent: :destroy
accepts_nested_attributes_for :product_options, allow_destroy: true, :reject_if => proc { |x| x[:option_name].blank? }
belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)
product_option.rb
class ProductOption < ActiveRecord::Base
belongs_to :product
end
Run Code Online (Sandbox Code Playgroud)
products_controller.rb
class ProductsController < ActionController::Base
layout "application"
def index
@products = Product.all
@current_user = Client.find_by(id: session[:client])
if @current_user.redeemed == true
redirect_to root_path
end
end
def show
@product = Product.find(params[:id])
@product_option = @product.product_options.find(params[:id])
@current_user = Client.find_by(id: session[:client])
@current_user.update(:product_option => @product_option.option_name)
@current_user.update(:selected_product => @product.id)
render :nothing => true
end
private …Run Code Online (Sandbox Code Playgroud) 我的环境:
我正在使用嵌套资源,并且无法确定在何处声明装饰器.
#app/controllers/users_controller.rb
def show
@user = UserDecorator.find(params[:id])
@items = @user.items
end
#app/controllers/items_controller.rb
def show
@item = @user.items.find(params[:id])
end
Run Code Online (Sandbox Code Playgroud)
我尝试更换items,ItemDecorator但没有用.我应该把它放在哪里?
我知道Draper在表单中存在嵌套资源的问题,但这不是表单!
我在Rails 3.2.5中做了一个嵌套的表单,但是当我添加accepts_nested_attributes_for我的fields_for消失时(他们只是停止在我的表单中显示).
这是我的模特:
class Product < ActiveRecord::Base
attr_accessible :title, :description, :variants_attributes
has_many :variants
accepts_nested_attributes_for :variants
validates :title, presence: true
end
Run Code Online (Sandbox Code Playgroud)
我的第二个模特是
class Variant < ActiveRecord::Base
belongs_to :product
attr_accessible :price, :sku, :weight, :inventory_quantity, :product_id
end
Run Code Online (Sandbox Code Playgroud)
在我看来,我有
<%= form_for [:admin, @product], :html => {:multipart => true} do |f| %>
<%= render 'admin/shared/error_messages', object: f.object %>
<fieldset>
<legend>Producto Nuevo</legend>
<div class="control-group">
<%= f.label :title, 'Título', class: 'control-label' %>
<div class="controls">
<%= f.text_field :title %>
</div>
</div>
**<%= f.fields_for :variants …Run Code Online (Sandbox Code Playgroud) ruby-on-rails nested-attributes fields-for ruby-on-rails-3 ruby-on-rails-3.2
在工作中,我有几个页面,每个页面都有相同位置的按钮,并具有相同的属性.每个页面也有细微差别.为此,我们创建了一个userControl模板并将所有按钮放入其中,然后将该用户控件应用于所有页面.但是,现在很难访问按钮并从每个页面的xaml修改它们,因为它们位于页面上的UserControl内..... 我如何优雅地访问每个页面的按钮?
我尝试过的:
目前,我们绑定了一堆依赖属性.我不喜欢这个选项,因为我有很多按钮,需要控制这些按钮上的很多属性.结果是数以百计的依赖属性,当我们需要改变某些东西时,它会变得非常混乱.
另一种方法是使用样式.我一般都喜欢这种方法,但由于这些按钮位于另一个控件内部,因此很难对其进行修改,并且模板一次只适用于一个按钮.
Adam Kemp发布了关于让用户在这里插入自己的按钮的消息,这就是我目前正在试图修改/修改的方法.不幸的是,我无法访问Xamarin.
虽然模板的代码运行时插入,模板不正确地更新按钮.如果我在MyButton Setter中放置一个断点,我可以看到该值实际上是一个空按钮,而不是我在主窗口中指定的那个.我该如何解决?
这是一些简化的代码:
我的模板UserControl的xaml:
<UserControl x:Class="TemplateCode.Template"
x:Name="TemplatePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="350"
d:DesignWidth="525"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Background="DarkGray">
<Grid>
<Button x:Name="_button" Width="200" Height="100" Content="Template Button"/>
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
我的模板UserControl的代码背后:
using System.Windows.Controls;
namespace TemplateCode
{
public partial class Template : UserControl
{
public static Button DefaultButton;
public Template()
{
InitializeComponent();
}
public Button MyButton
{
get
{
return _button;
}
set
{
_button = value; //I get …Run Code Online (Sandbox Code Playgroud) 当在rails 3上关注此http://railscasts.com/episodes/196-nested-model-form-part-1时,我收到此错误"警告:无法批量分配受保护的属性:races_attributes" .
Races是Events的一个组成部分.这是我的模特/ race.rb:
class Race < ActiveRecord::Base
belongs_to :event
attr_accessible :name, :unit
end
Run Code Online (Sandbox Code Playgroud)
这是我的models/event.rb:
class Event < ActiveRecord::Base
has_many :races, :dependent => :destroy
accepts_nested_attributes_for :races
attr_accessible :name, :date, :description, :location_name, :address_one, :address_two, :city, :state, :zip, :active, :races_attributes
end
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我目前正在尝试为模型创建一个表单,该模型具有动态数量的嵌套模型.我正在使用嵌套表单(如RailsCasts 197中所述).为了使事情变得更复杂,我的每个嵌套模型都has_one与第三个模型相关联,我也希望将其添加到表单中.
对于任何想知道过度规范化或不正确方法的人来说,这个例子是我面临的问题的简化版本.实际上,事情稍微复杂一点,这就是我们决定采取的方法.
一些示例代码来说明下面的问题:
#MODELS
class Test
attr_accessible :test_name, :test_description, :questions_attributes
has_many :questions
accepts_nested_attributes_for :questions
end
class Question
attr_accessible :question, :answer_attributes
belongs_to :test
has_one :answer
accepts_nested_attributes_for :answer
end
class Answer
attr_accessible :answer
belongs_to :question
end
#CONTROLLER
class TestsController < ApplicationController
#GET /tests/new
def new
@test = Test.new
@questions = @test.questions.build
@answers = @questions.build_answer
end
end
#VIEW
<%= form_for @test do |f| %>
<%= f.label :test_name %>
<%= f.text_box :test_name %>
<%= f.label :test_description %>
<%= f.text_area …Run Code Online (Sandbox Code Playgroud) 我试图在Rails 4中重现railscast#196.但是,我遇到了一些问题.
在我的例子中,我尝试生成一个电话簿 - 每个人可以有多个PhoneNumbers
这些是我的控制器的重要部分:
class PeopleController < ApplicationController
def new
@person = Person.new
3.times{ @person.phones.build }
end
def create
@person = Person.create(person_params)
@person.phones.build(params[:person][:phones])
redirect_to people_path
end
private
def person_params
params.require(:person).permit(:id, :name, phones_attributes: [ :id, :number ])
end
end
Run Code Online (Sandbox Code Playgroud)
这是我的新观点
<h1>New Person</h1>
<%= form_for :person, url: people_path do |f| %>
<p>
<%= f.label :name %> </ br>
<%= f.text_field :name %>
</p>
<%= f.fields_for :phones do |f_num| %>
<p>
<%= f_num.label :number %> </ br>
<%= f_num.text_field …Run Code Online (Sandbox Code Playgroud) 我正在使用jquery-fileupload-rails来上传多个文件.
我希望能够设置文档名称并向其添加多个附件.
但是现在当我选择3个附件时,documents每个附件创建3 个附件.
我想我需要改变某种形式来添加附件.我添加了多个选项和编码名称.
我想使用这个插件,因为稍后我会想要添加拖放功能.
从
= simple_form_for [:member, @document], html: { multipart: true } do |f|
= f.input :name
= f.simple_fields_for :attachments, Attachment.new do |a|
= a.file_field :attachment, multiple: true, name: "document[attachments_attributes][][attachment]"
= f.submit
Run Code Online (Sandbox Code Playgroud)
生成:
<input id="document_attachments_attributes_0_attachment" multiple="multiple" name="document[attachments_attributes][][attachment]" type="file">
Run Code Online (Sandbox Code Playgroud)
JS
jQuery ->
$('#new_document').fileupload()
Run Code Online (Sandbox Code Playgroud)
楷模
class Document < ActiveRecord::Base
has_many :attachments
accepts_nested_attributes_for :attachments
end
class Attachment < ActiveRecord::Base
belongs_to :document
has_attached_file :attachment
end
Run Code Online (Sandbox Code Playgroud)
调节器
class Member::DocumentsController < ApplicationController
def new
@document = Document.new
end …Run Code Online (Sandbox Code Playgroud) ruby jquery ruby-on-rails nested-attributes jquery-file-upload
对于以下情况,我使用嵌套表单遇到has_many/has_many关系问题.
我有文章,这可以有很多单位(Kg,袋等).通过我的表格,我需要记录这些数据.
所以我决定使用带有关系has_many的嵌套表单,但是我有一个错误: ArgumentError (No association found for name 'article_unit_of_measurements'. Has it been defined yet?)
这是我的Classes代码:
class Article < ActiveRecord::Base
has_many :unit_of_measurements, :through => :article_unit_of_measurements
accepts_nested_attributes_for :article_unit_of_measurements
end
class ArticleUnitOfMeasurement < ActiveRecord::Base
belongs_to :article
belongs_to :unit_of_measurement
accepts_nested_attributes_for :unit_of_measurement
end
class UnitOfMeasurement < ActiveRecord::Base
has_many :articles, :through => :article_unit_of_measurements
end
Run Code Online (Sandbox Code Playgroud)
在我的表单中使用jquery插件select2.js将我的选择单元显示为标签.
= simple_form_for([:shop, @article], html: {class: 'form-horizontal' }) do |f|
.col-md-10
= f.input :name, placeholder: "Name", :input_html => {:class => 'form-control'}, label: false
%label.col-md-2.control-label{for: "unit_of_measurement_id"} Unidad de medida
.col-md-10
%select#unit-select-measure.select2{:name =>"article[article_unit_of_measurements_attributes][#{Time.now.to_s}][unit_of_measurement_id]", …Run Code Online (Sandbox Code Playgroud) select has-many nested-forms nested-attributes ruby-on-rails-4
nested-forms ×3
ruby ×3
c# ×1
draper ×1
fields-for ×1
has-many ×1
hidden-field ×1
ignore ×1
jquery ×1
object ×1
select ×1
wpf ×1
xaml ×1