我的许多Backbone模型经常处理嵌套模型和集合,到目前为止我正在使用组合defaults,parse并toJSON手动实现嵌套:
ACME.Supplier = Backbone.Model.extend({
defaults: function() {
return {
contacts: new ACME.Contacts(),
tags: new ACME.Tags(),
attachments: new ACME.Attachments()
};
},
parse: function(res) {
if (res.contacts) res.contacts = new ACME.Contacts(res.contacts);
if (res.tags) res.tags = new ACME.Tags(res.tags);
if (res.attachments) res.attachments = new ACME.Attachments(res.attachments);
return res;
}
});
ACME.Tag = Backbone.Model.extend({
toJSON: function() {
return _.pick(this.attributes, 'id', 'name', 'type');
}
});
Run Code Online (Sandbox Code Playgroud)
我看了几个插件,基本上和上面一样,但是控制和更多的样板很少,所以我想知道是否有人对这个常见的Backbone.js问题有更优雅的解决方案.
编辑:我最终采用以下方法:
ACME.Supplier = Backbone.Model.extend({
initialize: function(options) {
this.tags = new ACME.Tags(options.tags);
},
parse: function(res) …Run Code Online (Sandbox Code Playgroud) 我有两个模型:我有一个嵌套的表单,允许用户输入@contact和@goal的属性.但是,当我去保存表单输入时,我收到以下错误:
1 error prohibited this contact from being saved:
Goals contact can't be blank
Run Code Online (Sandbox Code Playgroud)
以下是我的目标和联系模式,以及联系人控制器:
class Contact < ActiveRecord::Base
belongs_to :user
has_many :goals
accepts_nested_attributes_for :goals, allow_destroy: true
validates_presence_of :user_id,:name,:title,:email
end
class Goal < ActiveRecord::Base
belongs_to :contact
validates_presence_of :title, :due_date, :contact_id
end
class ContactsController < ApplicationController
before_action :set_contact, only: [:show, :edit, :update, :destroy]
# GET /contacts
# GET /contacts.json
def index
@contacts = current_user.contacts
end
# GET /contacts/1
# GET /contacts/1.json
def show
end
# GET /contacts/new
def new
@contact = …Run Code Online (Sandbox Code Playgroud) 所以我正在使用这个Railscast.
而且我知道Rails 4中的强参数有一些变化.
我已经四倍检查了我的实现,但无法看到我出错的地方.就目前而言,在最初提交患者时(即创建方法)勾选"销毁"框,按预期工作,并将删除任何具有复选框的药物,并允许任何不具有(从三种形式输入)它提供).
然而,当我随后编辑该患者时,任何未检查被删除的药物都是重复的(因此我最终得到的药物比我开始时更多),并且任何检查删除的药物似乎都没有改变.
因此,如果有两种药物附加"Med1"和"Med2",并且我编辑患者,如果两者都被标记为删除,我仍然会以"Med1"和"Med2"结束.如果仅将"Med1"标记为删除,我将以"Med1"和"Med2"以及额外的"Med2"结束.如果两者都没有标记为删除,我将最终得到两个"Med1"和"Med2".
#patient.rb
class Patient < ActiveRecord::Base
has_many :procedures
has_many :medications, dependent: :destroy
has_many :previous_operations, dependent: :destroy
accepts_nested_attributes_for :medications, :allow_destroy => true, :reject_if => lambda { |a| a[:name].blank? },
end
Run Code Online (Sandbox Code Playgroud)
#views/patients/_form.html.erb
<%= form_for(@patient) do |f| %>
<% if @patient.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@patient.errors.count, "error") %> prohibited this patient from being saved:</h2>
<ul>
<% @patient.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %> …Run Code Online (Sandbox Code Playgroud) ruby-on-rails nested-attributes ruby-on-rails-3 ruby-on-rails-4
我正在使用Rails基于一组复杂的嵌套属性自动神奇地创建子对象.因此,我需要以非常特殊的方式嵌套参数.显然我意识到我可以用JS构建它们,但是我希望表单的顺序自动帮助构造.对于上下文,我有2列,由2 <td>秒表示.每列可以创建新记录或编辑现有记录.当然,当要修改现有记录时,必须传递记录的id.
呈现的HTML如下:
<td width="50%" style="padding-right:3%" class="logistic-details" data-type="logistics" data-typelogistics="delivery" data-instructions="test instructions" data-id="1" data-amount="20">
<span class="area-to-inject-amount-inputs" data-object="type_logistics" data-type="logistics" data-typelogistics="delivery">
<input class="labeler-response" name="type_logistics_attributes[][id]" type="hidden" value="1">
<input class="labeler-response" name="type_logistics_attributes[][instructions]" type="text" value="test instructions">
</span>
</td>
<td width="50%" style="padding-right:3%" class="logistic-details" data-type="logistics" data-typelogistics="pickup" data-instructions="" data-id="" data-amount="0">
<span class="area-to-inject-amount-inputs" data-object="type_logistics" data-type="logistics" data-typelogistics="pickup" data-actioned="charged">
<input type="hidden" name="type_logistics_attributes[][type_of_logistics]" value="pickup">
<input class="injected-amount-input" type="number" min="0" max="" placeholder="Amount" name="type_logistics_attributes[][charged_amounts_attributes][][amount]" value="20">
<span class="area-to-inject-type-of-amount">
<input type="hidden" name="type_logistics_attributes[][charged_amounts_attributes][][type_of_amount]" value="logistics">
</span>
<input class="labeler-response" name="type_logistics_attributes[][instructions]" type="text" placeholder="Enter address and instructions">
</span>
</td>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,第一个<td>是修改id为1的现有记录,而第二个<td> …
html ruby-on-rails named-parameters nested-forms nested-attributes
我有以下型号
class Order < AR::Base
has_many :products
accepts_nested_attributes_for :products
end
class Product < AR::Base
belongs_to :order
has_and_belongs_to_many :stores
accepts_nested_attributes_for :stores
end
class Store < AR::Base
has_and_belongs_to_many :products
end
Run Code Online (Sandbox Code Playgroud)
现在我有一个订单视图,我想更新产品的商店.问题是我只想将产品连接到我的数据库中的现有商店,而不是创建新商店.
我在订单视图中的表单看起来像这样(使用Formtastic):
= semantic_form_for @order do |f|
= f.inputs :for => :live_products do |live_products_form|
= live_products_form.inputs :for => :stores do |stores_form|
= stores_form.input :name, :as => :select, :collection => Store.all.map(&:name)
Run Code Online (Sandbox Code Playgroud)
虽然它嵌套它工作正常.问题是,当我选择商店并尝试更新订单(以及产品和商店)时,Rails会尝试创建一个具有该名称的新商店.我希望它只使用现有商店并将产品连接到该商店.
任何帮助赞赏!
编辑1:
最后,我以一种非常粗暴的方式解决了这个问题:
# ProductsController
def update
[...]
# Filter out stores
stores_attributes = params[:product].delete(:stores_attributes)
@product.attributes = params[:product]
if stores_attributes.present?
# …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用角度JS,我想从我的rails应用程序中定义的嵌套资源中获取数据.
我写了以下几行:
UserMission = $resource("/users/:user_id/user_missions/:id", {user_id: "@user_id", id: "@id"}, {update: {method: "PUT"}})
$scope.user_missions = UserMission.query()
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Processing by UsersController#show as JSON
Parameters: {"id"=>"user_missions"}
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", "user_missions"]]
Completed 404 Not Found in 10ms
ActiveRecord::RecordNotFound (Couldn't find User with id=user_missions):
app/controllers/users_controller.rb:100:in `current_resource'
app/controllers/application_controller.rb:34:in `authorize'
Run Code Online (Sandbox Code Playgroud)
我的rails路由组织如下:
resources :users do
resources :user_missions
end
Run Code Online (Sandbox Code Playgroud)
我认为这归结于我不理解"@id".它说它来自angularjs网站的"数据对象" ,我不确定这意味着什么. …
json nested-forms nested-attributes ruby-on-rails-3 angularjs
我正在使用Devise和Rails 4开发一个Web应用程序.我有一个用户模型,我已经扩展了2个额外的表单字段,这样当用户注册时,他也可以提交他的名字/姓氏.(基于http://blog.12spokes.com/web-design-development/adding-custom-fields-to-your-devise-user-model-in-rails-4/).我现在想要添加一个机构模型.此模型has_many:用户和用户belongs_to:institution.我希望能够在注册用户的同一表格上注册该机构的名称.我知道我需要在我的Institution模型中使用nested_attribute ,因为这是父类,我将稍微展示一下.当我尝试注册用户时,我进入控制台:Unpermited参数:机构.
我的提示是我无法根据我的子类(User)更新我的父类(Institution).可能有解决方案吗?或者有没有人经历类似的事情?
class Institutions < ActiveRecord::Base
has_many :users,
accepts_nested_attributes_for :users
end
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :institution
end
Run Code Online (Sandbox Code Playgroud)
registrations/new.html.erb这里我有嵌套表格
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
.
.
<%= f.fields_for :institutions do |i| %>
<p><%= i.label :name %><br />
<%= i.text_field :institutions_attr %></p>
<% end %>
Run Code Online (Sandbox Code Playgroud)
基于我之前链接的教程,我创建了一个新的 …
ruby-on-rails nested-forms nested-attributes devise strong-parameters
我坚持这个简单的选择任务.我有这个型号:
# id :integer(4) not null, primary key
# category :string(255)
# content :text
class Question < ActiveRecord::Base
has_many :choices, :dependent => :destroy
accepts_nested_attributes_for :choices
end
# id :integer(4) not null, primary key
# content :text
# correct :boolean(1)
# question_id :integer(4)
class Choice < ActiveRecord::Base
belongs_to :question
end
Run Code Online (Sandbox Code Playgroud)
当我创建一个新的问题,我想指定不仅在嵌套形式content的Question,但即使是content3个的Answer对象,并用一个单选按钮哪一个是选择correct答案.在new控制器的动作中,我有这个:
def new
@title = "New Question"
@question = Question.new
3.times { @question.choices.build }
respond_to do |format|
format.html # …Run Code Online (Sandbox Code Playgroud) 我有
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) nested-forms ×5
angularjs ×1
backbone.js ×1
devise ×1
hidden-field ×1
html ×1
ignore ×1
json ×1
radio-button ×1
ruby ×1