我有以下型号
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) 我试图在嵌套表单中的文件字段上使用HTML5多重属性.
模型如下:
class Album < ActiveRecord::Base
has_many :album_images
has_many :images, :through => :album_images
accepts_nested_attributes_for :images
end
class Image < ActiveRecord::Base
has_many :album_images
has_many :albums, :through => :album_images
mount_uploader :filename, ImageUploader
validates_presence_of :filename
end
Run Code Online (Sandbox Code Playgroud)
风景:
<%= semantic_form_for @album, :url => upload_path do |f| %>
<%= f.inputs do %>
<%= f.input :name, :label => 'Album title' %>
<% end %>
<%= f.input :images, :as => :file, :input_html => {:multiple => true} %>
<%= f.buttons do %>
<%= f.commit_button 'Upload' %>
<% end …Run Code Online (Sandbox Code Playgroud) 我正在使用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) 错误:
param is missing or the value is empty: color
Run Code Online (Sandbox Code Playgroud)
我正在制作一个表单,我可以将嵌套属性添加到父记录,我想通过复选框添加它们.我有父模型"汽车"和儿童模型"颜色"......但我想从一些默认的"颜色"开始...所以我也有模型"Sample_Colors",它根据"Car_Models"改变.
我正在尝试使用复选框将多个"颜色"添加到关联的"汽车"...我不希望与"Sample_Colors"和"颜色"的HABTM关系,因为我需要"颜色"记录可编辑而不仅仅是一个连接表.我使用HABTM完成了这个,所以我真的不明白为什么我不能以类似的方式创建非连接表记录.
我遇到麻烦的地方是质量分配......它要么抛出上面的错误,要么找不到colors_id ......
来澄清我要做的事情:
复选框需要
1.创建一个新的"颜色"记录(@ color.new)与@car记录父项关联
2.将@ color.new记录
的"value_one"列设置为sample_color.value_one值3.设置@的"value_two"列color.new记录到sample_color.value_two值
4.创建的复选框数量= =迭代的@sample_colors.
car_model.rb
class CarModel
has_many :sample_colors, dependent: :destroy
has_many :cars, dependent: :destroy
Run Code Online (Sandbox Code Playgroud)
car.rb
class Car
has_many :colors, dependent: :destroy
belongs_to :car_model
accepts_nested_attributes_for :colors, allow_destroy: true
Run Code Online (Sandbox Code Playgroud)
sample_color.rb
class SampleColor
belongs_to :car_model
Run Code Online (Sandbox Code Playgroud)
color.rb
class Color
belongs_to :car
accepts_nested_attributes_for :finishes, allow_destroy: true
Run Code Online (Sandbox Code Playgroud)
_form(用于添加颜色)
<%= form_for @car do |f| %>
<%= f.fields_for 'car[color_attributes][]', @color, index: nil do |f| %>
<label class="form-label dk-aqua">Colors …Run Code Online (Sandbox Code Playgroud) 我想得到一些嵌套的参数.我有一个包含许多物品的订单,这些物品都有一个类型.我想从控制器创建方法获取type_id参数.
@order = Order.new(params[:order])
@order.items.each do |f|
f.item_type_id = Item_type.find_by_name(f.item_type_id).id
end
Run Code Online (Sandbox Code Playgroud)
原因是我希望用户能够在视图中创建新的item_types.当他们这样做时,我使用AJAX调用将它们添加到数据库中.当他们发布表单时,我在item_type_id参数中获取item_type的名称,我想找到正确的item_type并将id设置为
我收到以下错误:
ActiveRecord::AssociationTypeMismatch in ContractsController#create
ExchangeRate(#2183081860) expected, got HashWithIndifferentAccess(#2159586480)
Params:
{"commit"=>"Create",
"authenticity_token"=>"g2/Vm2pTcDGk6uRas+aTgpiQiGDY8lsc3UoL8iE+7+E=",
"contract"=>{"side"=>"BUY",
"currency_id"=>"488525179",
"amount"=>"1000",
"user_id"=>"633107804",
"exchange_rate"=>{"rate"=>"1.7"}}}
Run Code Online (Sandbox Code Playgroud)
我的相关模型是:
class Contract < ActiveRecord::Base
belongs_to :currency
belongs_to :user
has_one :exchange_rate
has_many :trades
accepts_nested_attributes_for :exchange_rate
end
class ExchangeRate < ActiveRecord::Base
belongs_to :denccy, :class_name=>"Currency"
belongs_to :numccy, :class_name=>"Currency"
belongs_to :contract
end
Run Code Online (Sandbox Code Playgroud)
我的观点是:
<% form_for @contract do |contractForm| %>
Username: <%= contractForm.collection_select(:user_id, User.all, :id, :username) %> <br>
B/S: <%= contractForm.select(:side,options_for_select([['BUY', 'BUY'], ['SELL', 'SELL']], 'BUY')) %> <br>
Currency: <%= contractForm.collection_select(:currency_id, Currency.all, :id, :ccy) %> <br> <br>
Amount: <%= …Run Code Online (Sandbox Code Playgroud) 我通常不使用表格来表单,但是当使用嵌套表单时(使用nested_form或cocoon gem时),是否可以将每组表单元素放在表格行中?
对我来说,这看起来非常直观:表中的每一行代表一个对象.但是默认情况下,nested_form和cocoon gem都没有在它们的示例中添加表,所以我想知道使用表单是不是最好的方法?
我似乎无法获得嵌套属性来保存到数据库.我正在使用Rails 4.
这是我的模特:
class Answer < ActiveRecord::Base
belongs_to :question
end
class Question < ActiveRecord::Base
has_many :answers
belongs_to :survey
accepts_nested_attributes_for :answers, allow_destroy: true
end
class Survey < ActiveRecord::Base
has_many :questions
validates_presence_of :name
accepts_nested_attributes_for :questions
end
Run Code Online (Sandbox Code Playgroud)
这是控制器:
def create
@survey = Survey.new(survey_params)
respond_to do |format|
if @survey.save
format.html { redirect_to @survey, notice: 'Survey was successfully created.' }
format.json { render action: 'show', status: :created, location: @survey }
else
format.html { render action: 'new' }
format.json { render json: @survey.errors, status: :unprocessable_entity } …Run Code Online (Sandbox Code Playgroud) 我一直在观看和重现我的应用程序上的这些railscast:196-nested-model-form-part-1和197-neested-model-form-part-2.我还没有专业帐户,所以我无法观看修改后的剧集.
我正在rails4(edge)下开发并且link_to_function已经被弃用而不喜欢不引人注目的JS(这很棒).
我将保留上述railscast的示例(即调查/问题).我想做的是使用问题的部分通过不引人注目的JavaScript,我只是不知道我应该如何以及在哪里做到这一点.
我想到了两种方法:
app/assets/javascripts的文件surveys.js与功能,add_question但我不知道如何从这里使用我的部分.SurveyController使用部分问题作出回应,但我对调查控制器中的问题采取特定行动的事实感到困扰.我不禁认为必须有更好的方法来做到这一点.
nested-forms ×10
ajax ×1
checkbox ×1
cocoon-gem ×1
devise ×1
formtastic ×1
html ×1
html5 ×1
parameters ×1
radio-button ×1
railscasts ×1
ruby ×1