我正试图弄清楚这一点.我有以下表格,效果很好!
<%= bootstrap_form_for @template do |f| %>
<%= f.text_field :prompt, :class => :span6, :placeholder => "Which one is running?", :autocomplete => :off %>
<%= f.select 'group_id', options_from_collection_for_select(@groups, 'id', 'name') %>
<% 4.times do |num| %>
<%= f.fields_for :template_assignments do |builder| %>
<%= builder.hidden_field :choice_id %>
<%= builder.check_box :correct %>
<% end %>
<% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
然后我有我的模型:
class Template < ActiveRecord::Base
belongs_to :group
has_many :template_assignments
has_many :choices, :through => :template_assignments
accepts_nested_attributes_for :template_assignments, :reject_if => lambda { …Run Code Online (Sandbox Code Playgroud) 在嵌套表单的更新操作期间,它不是更新当前的嵌套记录,而是创建新的嵌套记录。
这是我的控制器的样子:
class ApplicationsController < ApplicationController
before_filter :set_user_and_job
def new
job = params[:job_id]
@application = Application.build(job)
end
def create
@application = Application.new(application_params)
@application.save
redirect_to root_url, :notice => "You have now applied!"
end
def edit
@application = Application.find(params[:id])
@answers = []
@job.questions.each do |question|
@application.answers.each do |answer|
@answers << answer if answer.question_id == question.id
end
end
end
def update
@application = Application.find(params[:id])
@application.update_attributes(application_params)
redirect_to root_url, :notice => "You have updated your application!"
end
def destroy
Application.find(params[:id]).destroy
flash[:success] = "Application Deleted."
redirect_to …Run Code Online (Sandbox Code Playgroud) 在 Rails ( 4.1.5 / ruby 2.0.0p481 / win64 ) 应用程序中,我有 Student 和 Course 之间的多对多关系和表示关联的连接模型 StudentCourse,它有一个名为“started”的附加属性,它默认设置为“false”。
我还在由 student_id 和 course_id 组成的连接表中添加了一个索引,并设置了一个唯一的检查,就像这样
t.index [:student_id, :course_id], :unique => true, :name => 'by_student_and_course'
Run Code Online (Sandbox Code Playgroud)
现在我看到关联是通过以下任一方式创建的:
Student.first.courses.create(:name => "english")
Run Code Online (Sandbox Code Playgroud)
或者
Course.first.students << Student.first
Run Code Online (Sandbox Code Playgroud)
这很好,我想这是预期的行为。
我正在寻找的是获取和设置“开始”属性的正确方法。 从其他模型而不是直接从连接模型访问该属性时,我看到了一个奇怪的行为。
s = Student.create
c = Course.create(:name => "english")
s.student_courses.first
Run Code Online (Sandbox Code Playgroud)
=> | "英语" | 假| #(为了实用,以表格的形式表示)
s.student_courses.first.started = true
Run Code Online (Sandbox Code Playgroud)
=> | "英语" | 真实|
s.save
Run Code Online (Sandbox Code Playgroud)
=> 真
好的,这看起来已经保存了,但是当我抢劫 ak 时:
StudentCourse.first
Run Code Online (Sandbox Code Playgroud)
=> | 1 | 1 | 假|
因此,如果我通过学生嵌套属性,则将其设置为 true,但在连接模型中它仍然为 …
ruby activerecord many-to-many ruby-on-rails nested-attributes
好的伙计们,Rails 5 确实有其细微差别与 Rails 4 不同。我所要做的是,每次我单击表单上的提交按钮时,它都会重新加载错误Profile user must exist and Profile user can't be blank。该表单加载良好,包括嵌套模型表单,但无论出于何种原因,它都无法在尝试将具有以下输出的子模型保存到控制台之前保存父模型:
Puma starting in single mode...
* Version 3.7.0 (ruby 2.2.6-p396), codename: Snowy Sagebrush
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
Started POST "/users" for 192.168.0.31 at 2017-03-09 18:51:04 -0500
Cannot render console from 192.168.0.31! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
ActiveRecord::SchemaMigration Load (0.2ms) SELECT `schema_migrations`.* FROM `schema_migrations`
Processing by UsersController#create as HTML
Parameters: …Run Code Online (Sandbox Code Playgroud) ruby nested-forms nested-attributes nested-form-for ruby-on-rails-5
我有一个has_one配置文件的用户模型,该配置文件属于该用户.该配置文件有一个type用于单表继承的列,可以是"artist"或"listener".我希望用户在新用户视图中注册时设置此项.因此我有以下形式的代码:
<%= f.fields_for :profile do |t| %>
<div class ="field">
<%= t.label :type, "Are you an artist or listener?" %><br />
<%= t.radio_button :type "artist" %>
<%= t.radio_button :type "listener" %>
</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)
这在我的用户模型中:
accepts_nested_attributes_for :profile
Run Code Online (Sandbox Code Playgroud)
但我得到他的错误:
ArgumentError in Videos#index
Showing /rubyprograms/dreamstill/app/views/videos/_video.html.erb where line #3 raised:
No association found for name `profile'. Has it been defined yet?
Run Code Online (Sandbox Code Playgroud)
为什么会这样,我该如何解决?
另外我很困惑为什么错误会在我的视频部分中引出第3行,这根本就没有提到profile......
更新:
这是整个表格:
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(@user.errors.count, …Run Code Online (Sandbox Code Playgroud) 在我的控制器创建方法中,我使用accepts_nested_attributes创建父和子(ren)对象.一切正常.
子项具有正确设置的排序属性.
但是,当验证失败时(对于缺少的属性说),当fields_for方法运行时,不会保留子对象的顺序.
我尝试过使用parent.children.reorder("订购ASC"),但这不起作用......
我很乐意发布任何代码,如果它让事情更清楚!
def create
@parent = Parent.new(params[:parent])
respond_to do |format|
if @parent.save
format.html
else
@parent.children.reorder("ordering ASC") #this makes no difference
format.html { render :action => "new" }
end
end
end
Run Code Online (Sandbox Code Playgroud)
并以部分形式
<%= f.fields_for :children do |ff| %>
<%= render "child_fields", :ff => ff %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
任何指针都会很棒..
无论我做什么,我的朋友的嵌套表单字段都不会显示..我相信,accept_nested_attributes设置是否正确?
views/user_steps/show.html.erb
<%= simple_form_for @user do |f| %>
<%= f.input :city %>
<%= f.input :address %>
<%= f.input :zipcode %>
<%= f.input :date_of_birth %>
<%= f.input :gender, :collection => ['male','female'] %>
<%= f.association :interests, :as => :check_boxes, :label => false %>
<%= f.association :holidays, :as => :check_boxes, :label => false %>
<%= f.simple_fields_for :friends do |friend_f| %>
<%= friend_f.input :name %>
<%= friend_f.input :dob %>
<%= friend_f.input :gender %>
<% end %>
<%= f.button :submit %>
<%end%>
class UserStepsController < …Run Code Online (Sandbox Code Playgroud) ruby-on-rails associations nested-attributes ruby-on-rails-3 simple-form
在rails中,update_attributes在模型上使用将创建基于的嵌套模型association_attributes.是否有一种惯用的方法来使其更新嵌套模型?
例如:
Message.rb:
attr_accessible :recipient_attributes
has_one :recipient
accepts_nested_attributes_for :recipient
Run Code Online (Sandbox Code Playgroud)
Recipient.rb
belongs_to :message
# has an name fied
# has an email field
Run Code Online (Sandbox Code Playgroud)
接受者
r = Recipient.create
r.create_recipient name: "John Smith", email: "john@gmail.com"
r.update_attributes recipient_attributes: {email: "johns_new_address@gmail.com"}
r.recipient.name # nil <-- this creates a NEW recipient, so the name is nil
r.recipient.email # johns_new_address@gmail.com
Run Code Online (Sandbox Code Playgroud)
相反,我希望r.recipient收到相同的收件人记录,但会收到一封新电子邮件.
我有3个模型:用户,答案和问题。
user.rb
has_many :questions
has_many :answers
Run Code Online (Sandbox Code Playgroud)
Question.rb
has_many :answers
belongs_to :user
accept_nested_attributes_for :answers
Run Code Online (Sandbox Code Playgroud)
answer.rb
belongs_to :question
belongs_to :user
Run Code Online (Sandbox Code Playgroud)
在questions / show.html.erb中
form_for @question do |f|
f.fields_for :answers, @question.answers.build do |builder|
builder.text_area, :body
end
f.submit
end
Run Code Online (Sandbox Code Playgroud)
Submit调用问题#update动作,由于嵌套了资源,新答案将被保存在数据库中。我想知道:user_id提交问题后,如何将答案列保存在数据库中?提交表格后,我能以某种方式通过current_user.id答案user_id栏吗?
我已经为我的@miniature模型工作了新的/创建动作和表单,它是嵌套模型@scales.我无法正确进行更新/编辑操作.应该很简单,但我很困难.
@miniaturehas_many @scales通过@sizes.
在我的@miniature模型中,我有
has_many :sizes, dependent: :destroy
has_many :scales, :through => :sizes
accepts_nested_attributes_for :sizes, allow_destroy: true
Run Code Online (Sandbox Code Playgroud)
在控制器中我有
def new
@miniature = Miniature.new
@all_scales = Scale.all
@size = @miniature.sizes.build
end
def create
@miniature = Miniature.new(miniature_params)
params[:scales][:id].each do |scale|
if !scale.empty?
@miniature.sizes.build(:scale_id => scale)
end
end
if @miniature.save
redirect_to miniature
else
render 'new'
end
end
private
def miniature_params
params.require(:miniature).permit(:name, :release_date, :material, :pcode, :notes, sizes_attributes: [:id, :scale_id, :miniset_id])
end
Run Code Online (Sandbox Code Playgroud)
这有效但编辑和更新操作却没有.我的编辑设置与我的编辑相同.
def edit …Run Code Online (Sandbox Code Playgroud) ruby ×3
activerecord ×2
nested-forms ×2
associations ×1
controller ×1
fields-for ×1
idioms ×1
many-to-many ×1
simple-form ×1