我有一个嵌套属性的模型:
class Foo < ActiveRecord::Base
has_many :bar
accepts_nested_attributes_for :bar
end
Run Code Online (Sandbox Code Playgroud)
它工作正常.但是我想确保每一个Foo,我至少有两个Bar.我无法访问bar_attributes我的验证,所以我似乎无法验证它.
有没有干净的方法呢?
什么相当于<%= f.hidden_field :_destroy %>nullify而不是毁灭?(即我只是将它从关联中删除,但我不想破坏它).
一个示例情况是:
class Foo < ActiveRecord::Base
has_many :bar, :dependent=>:nullify, :autosave=>true
accepts_nested_attributes_for :bar, :reject_if => proc { |attributes| attributes.all? {|k,v| v.blank?} }
class Bar < ActiveRecord::Base
belongs_to :foo
Run Code Online (Sandbox Code Playgroud)
在Foo中edit.html.erb:
<%= f.fields_for :bar do |builder| %>
<%= builder.some_rails_helper %>
<%= builder.hidden_field :_remove #<-- set value to 1 to destroy, but how to unassociate?%>
<% end %>
Run Code Online (Sandbox Code Playgroud)
对解决方案的一个小修改
def remove
#!self.foo_id.nil? should be:
false #this way newly created objects aren't destroyed, and neither are existing ones. …Run Code Online (Sandbox Code Playgroud) 也许我不知道如何询问/搜索这个特定的东西,但基本上我想在创建父对象时创建一些相关的模型...说我有以下情况:
我有Recipe哪些has_many Ingredient车型?有没有一种方法,使他们一次全部,说这是我的例子种子任务的一部分:
Recipe.create({
:title => 'apple pie',
:description => 'just apple pie',
:ingredients => {
[0] => {:title => 'apples'},
[1] => {:title => 'sugar'},
[2] => {:title => 'pie crust'}
}
})
Run Code Online (Sandbox Code Playgroud)
或者就像我完全疯了一样?必须有某种方式来做同样的事情,不创建父模型,然后所有的孩子......等等.
我阅读了很多关于`has_one关系和嵌套属性的页面,但是没有成功地完成这项工作.任何帮助都会很棒.
每个用户都有_one网络.我正在尝试以一种形式收集两个属性的信息,但不断获得异常ActiveRecord::AssociationTypeMismatch in UsersController#create
传递的参数是:
{"utf8"=>"✓",
"authenticity_token"=>"I54tm1ovzHEHaXbBLTT+5tqBJv2795sKg978ot3HDBc=",
"user"=>{"name"=>"Bilbo Baggins",
"email"=>"bilbo@lotr.com",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]",
"network"=>{"home_lng"=>"-87.91894912719727",
"home_lat"=>"43.03812464542969",
"center_lng"=>"-87.91894912719727",
"center_lat"=>"43.03812464542969",
"radius"=>"500"}},
"commit"=>"Sign up"}
Run Code Online (Sandbox Code Playgroud)
我猜测参数Network必须以某种方式出现,network_attributes但我不知道如何.
控制器:
def create
@user = User.new(params[:user])
if (@user.save)
sign_in @user
flash[:success] = "One ring to rule them all!"
redirect_to @user
else
@title = "The journey begins..."
render 'new'
end
end
Run Code Online (Sandbox Code Playgroud)
视图:
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %><br />
<%= f.label :password %>
<%= f.password_field :password …Run Code Online (Sandbox Code Playgroud) ruby-on-rails nested-forms nested-attributes ruby-on-rails-3
我遇到嵌套属性和Devise的问题.与如何在设计模型中使用嵌套属性类似的问题.据我所知,我已按照此处的建议设置了所有内容: 覆盖设计注册控制器
我已经设置了关联的用户和订阅,我已经"accepts_nested_attributes_for"和包括:subscriptions_attributes中attr_accessible,但我从设计控制器警告.
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,:recoverable, :rememberable, :trackable, :validatable
validates_presence_of :first_name, :last_name
has_many :subscriptions
accepts_nested_attributes_for :subscriptions
attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :remember_me, :subscriptions_attributes
...
end
Run Code Online (Sandbox Code Playgroud)
-
class Subscription < ActiveRecord::Base
belongs_to :user
validates_presence_of :user_id, :chargify_subscription_id, :chargify_product_handle
attr_accessible :user_id, :chargify_subscription_id, :chargify_product_handle
...
end
Run Code Online (Sandbox Code Playgroud)
设计/注册/ new.html.erb:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<p><%= f.label :first_name %><br />
<%= f.text_field :first_name %></p> …Run Code Online (Sandbox Code Playgroud) 我正在使用Rails 3.2.6,这是一个示例案例:
class Man < ActiveRecord::Base
has_many :eyes
accepts_nested_attributes_for :eyes
end
class Eye < ActiveRecord::Base
belongs_to :man
validates_inclusion_of :color, in: { %w[brown green blue] }
end
Run Code Online (Sandbox Code Playgroud)
意见(在HAML中):
= form_for @man do |f|
- if @man.errors.any?
#error_explanation
%h2= t 'errors.messages.record_invalid', count: @man.errors.count
%ul
- @man.errors.full_messages.each do |msg|
%li= msg
= f.fields_for(:eyes) do |b|
.field
= b.label :color
= b.text_field :color
.actions
= f.submit :submit
Run Code Online (Sandbox Code Playgroud)
it.yml:
it:
activerecord:
attributes:
customer:
eyes: Occhi
customer/eyes:
color: Colore
errors:
models:
man/eyes:
attributes:
color:
inclusion: non …Run Code Online (Sandbox Code Playgroud) activerecord internationalization nested-attributes ruby-on-rails-3 rails-i18n
我正在尝试在我的主页上显示评论列表,同时显示每个评论的评分.
我想要一个逗号分隔的每个评论附带的不同评级列表(基于相关的review_id); 评级是-5到5的整数.加载页面时我得到的是:
Review 1. #<ActiveRecord::Relation:0x007f9879749a50>
Review 2. #<ActiveRecord::Relation:0x007f9879749a50>
Review 3. #<ActiveRecord::Relation:0x007f9879749a50>
Review 4. #<ActiveRecord::Relation:0x007f9879749a50>
Review 5. #<ActiveRecord::Relation:0x007f9879749a50>
Run Code Online (Sandbox Code Playgroud)
当我想看到这个:
Review 1. 1, 5, 2, -3
Review 2. 2, -1
Review 3. 1, 1, 4, 5
Review 4. -5, -2, -3
Review 5. 1, 1, 3, 2, 1, 5, 3, 2
Run Code Online (Sandbox Code Playgroud)
用户模型:
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :ratings_attributes, :reviews_attributes
has_many :reviews, dependent: :destroy
has_many :ratings, :through => :reviews, dependent: :destroy
accepts_nested_attributes_for :ratings, allow_destroy: true
accepts_nested_attributes_for :reviews, …Run Code Online (Sandbox Code Playgroud) activerecord controller ruby-on-rails associations nested-attributes
我正在尝试创建一篇文章.
class Article < ActiveRecord::Base
belongs_to :article_skill
attr_accessible :articles_skill_attributes
accepts_nested_attributes_for :articles_skill
end
class ArticlesSkill < ActiveRecord::Base
attr_accessible :description, :name
has_many :articles
end
Run Code Online (Sandbox Code Playgroud)
这是我的形式 article/new.html.erb
<%= article_form.fields_for :articles_skill, @article.articles_skill do |b|%>
<label class="medium"><span class="red">*</span> Skill</label>
<%= b.select :id, options_for_select(ArticlesSkill.all.collect{|m| [m.name, m.id]}) %>
<%end%>
Run Code Online (Sandbox Code Playgroud)
这article_form是@article表单对象的构建器.
如果我尝试保存@article对象,则显示此错误.
Couldn't find ArticlesSkill with ID=1 for Article with ID=
Run Code Online (Sandbox Code Playgroud) 在Rails 3.2应用程序中,我使用简单表单来创建复杂的表单.
表单/模型accepts_nested_attributes_for,我需要获取子对象的索引.
型号:
class Project
has_many :tasks
accepts_nested_attributes_for :tasks
end
class Task
belongs_to :project
end
Run Code Online (Sandbox Code Playgroud)
表格
<%= simple_form_for @project do |f| %>
<%= f.simple_fields_for :tasks do |builder| %>
## I need to get the index of each object built via builder
<% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
我如何正确获取索引?
我正在使用嵌套的params(通过hansack嵌套为q)和普通的params一起在页面上构建链接,当我尝试将嵌套的params与其他params合并时,我很难让两者完美地玩.
例如,如果我有:
{"freq"=>"weekly", "loan_amount"=>"350000",
"q"=>{"lowEquity_true"=>"1", "s"=>"rate asc"}}
Run Code Online (Sandbox Code Playgroud)
并尝试构建一个链接来更改param"lowEquity_true"
rates_url(params[:q].merge(:lowEquity_true => '0'))
Run Code Online (Sandbox Code Playgroud)
然后我最终得到了下面的新参数,看起来像是更新了q但是丢掉了其余的参数.
{"lowEquity_true"=>"0", "s"=>"rate asc"}
Run Code Online (Sandbox Code Playgroud)
如果我试图将q&merge合并到其他参数中,它不会更新q,只是将q中的内容合并到其他参数中
rates_url(params[:q].merge(:lowEquity_true => '0').merge(params))
{"freq"=>"weekly", "loan_amount"=>"350000", "lowEquity_true"=>"0",
"q"=>{"lowEquity_true"=>"1", "s"=>"rate asc"},
"s"=>"rate asc"}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了各种各样的组合,似乎没有任何地方,所以我确信我错过了一些基本的东西!
activerecord ×2
associations ×1
controller ×1
devise ×1
form-for ×1
merge ×1
nested-forms ×1
params ×1
rails-i18n ×1
ransack ×1
ruby ×1
simple-form ×1