标签: nested-attributes

rails中的label_tag和checkbox_tag问题

我希望我的复选框可用,所以我通常会在复选框中添加标签,这样您就可以选择文本而不必"瞄准"复选框.

问题是,如果我在rails中使用嵌套属性表单怎么办?我现在有这个代码:

%ul
  - test_category.test_cases.each do |test_case|
    %li
      = check_box_tag "job_test_cases[]", "#{test_case.id}", test_case.id
      = label_tag "test_case_#{test_case.id}", test_case.name
Run Code Online (Sandbox Code Playgroud)

这个问题是它产生了这个:

<li>
  <input type="checkbox" value="70" name="job_test_cases[]" id="job_test_cases_" checked="checked">
  <label for="test_case_70">Blah blah</label>
</li>
Run Code Online (Sandbox Code Playgroud)

而我希望它是这样的:

<li>
  <input type="checkbox" value="70" name="test_case_id[]" id="test_case_70" checked="checked">
  <label for="test_case_70">Blah BLah blah/label>
</li>
Run Code Online (Sandbox Code Playgroud)

checkbox label nested-attributes ruby-on-rails-3

9
推荐指数
2
解决办法
1万
查看次数

为什么Backbone模型会在保存时向服务器发送重复属性?

我正在编写一个练习Backbone应用程序,使用Rails后端API,我对在Backbone模型上保存的行为感到困惑.

假设一个团队有很多玩家,我想在一个POST中保存一个拥有众多玩家的团队.

所以在Rails中我有:

class Team < ActiveRecord::Base
  has_many :players
  accepts_nested_attributes_for :players
end

class Player < ActiveRecod::Base
  belongs_to :team
end
Run Code Online (Sandbox Code Playgroud)

对于骨干客户端,我有一个Player模型和一个Players集合定义(未显示)

然后是包含团队模型(注意:没有团队集合)

Demo.Models.Team = Backbone.Model.extend({
  urlRoot: '/teams',
  defaults: {
    'team_size': 12
  },
  initialize: function() {
    this.players = new Demo.Collections.Players());
  },
  toJSON: function() {
    var json = _.clone(this.attributes);
    json.players_attributes = this.players.map(function(player) {
      return player.toJSON();
    });
    return json;  
  }
}
Run Code Online (Sandbox Code Playgroud)

当我在浏览器中检查我的字符串化JSON时,一切看起来都很好:

{"team_size":12, "players_attributes":[{"name":"Fred"},{"name":"Jim" },{"name":"Mark"}]}
Run Code Online (Sandbox Code Playgroud)

检查服务器日志,重复一次顶级属性('团队大小'),一次在顶层,然后在根密钥下重复.

Started POST "/teams" for 127.0.0.1 at 2012-06-07 13:39:40 -0400
Processing by TeamsController#create as JSON
  Parameters: {
    "team_size"=>12, "players_attributes":[{"name":"Fred"},{"name":"Jim" …
Run Code Online (Sandbox Code Playgroud)

javascript ruby ruby-on-rails nested-attributes backbone.js

9
推荐指数
1
解决办法
1369
查看次数

Rails - 更新nested_attributes不适用于一对多关系

我正在尝试实现nested_attributes并创建一个计划正在运行.但它在编辑时失败而没有出现任何错误.它适用于一对一的关系,但不适用于一对多的关系.我正在传递日程表的ID,正如本文档中所述:NestedAttributes

我有这些模型:

class Article < ActiveRecord::Base
  has_many :schedules

  accepts_nested_attributes_for :schedules
end

class Schedule < ActiveRecord::Base
  belongs_to :article
end
Run Code Online (Sandbox Code Playgroud)

这里是控制器的片段,用于将强参数列入白名单:

def article_params
  params.require(:article).permit(schedules_attributes: [ :id, :schedule_for, :content, :time_zone, :date, :time ])
end
Run Code Online (Sandbox Code Playgroud)

这是我通过rails控制台尝试的示例:

article_params = {"schedules_attributes"=>{"0"=>{"schedule_for"=>"pre", "content"=>"This is a scheduled message for ETS - !!!!!!!", "time_zone"=>"American Samoa", "date"=>"2013-10-20", "time"=>"11:15 AM", "id"=>"1"}}}

article = Article.find(1)

article.update(article_params)

D, [2013-10-25T16:42:50.266517 #2296] DEBUG -- :    (0.1ms)  BEGIN
D, [2013-10-25T16:42:50.267789 #2296] DEBUG -- :   Schedule Load (0.3ms)  SELECT "schedules".* …
Run Code Online (Sandbox Code Playgroud)

activerecord ruby-on-rails nested-attributes ruby-on-rails-4

9
推荐指数
1
解决办法
1782
查看次数

Rails:摆脱泛型"X无效"验证错误

我有一个注册表单,它有嵌套的关联/属性,无论你想要什么.

我的层次结构是这样的:

class User < ActiveRecord::Base
  acts_as_authentic
  belongs_to :user_role, :polymorphic => true
end

class Customer < ActiveRecord::Base
  has_one :user, :as => :user_role, :dependent => :destroy
  accepts_nested_attributes_for :user, :allow_destroy => true
  validates_associated :user
end

class Employee < ActiveRecord::Base
  has_one :user, :as => :user_role, :dependent => :destroy
  accepts_nested_attributes_for :user, :allow_destroy => true
  validates_associated :user
end
Run Code Online (Sandbox Code Playgroud)

我在这些课程中也有一些验证内容.我的问题是,如果我尝试使用空白表单创建和客户(或员工等),我会得到所有的验证错误,加上一些通用的错误,如"用户无效"和"客户无效"如果我迭代错误我得到的东西:

user.login can't be blank
User is invalid
customer.whatever is blah blah blah...etc
customer.some_other_error etc etc
Run Code Online (Sandbox Code Playgroud)

由于嵌套用户模型中至少有一个无效字段,因此会在错误列表中添加额外的"X无效"消息.这让我的客户感到困惑,所以我想知道是否有一个快速的方法来做到这一点,而不是自己提交错误.

validation ruby-on-rails nested-attributes

8
推荐指数
1
解决办法
3496
查看次数

rails simple_nested_form_for fields_for错误的参数数量

所以我正在使用rails 3.1构建一个表单

<%= simple_nested_form_for(@person, :url => collection_url, :html=>{:multipart => true}) do |f| %>
  <%= render :partial => "form", :locals => { :f => f } %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

但部分中的这一行导致了问题:

<h2>Badges</h2> 
<ul id="certifications">
// this following line is raising the error "wrong number of arguments (4 for 3)"
<%= f.fields_for :certifications do |certification_form| %> 
    <%= render :partial => 'certification', :locals => { :f => certification_form } %>
<% end %>
</ul>
<%= f.link_to_add "Add a Badge", :certifications %>
Run Code Online (Sandbox Code Playgroud)

所以这是模型:

class …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails actionview nested-forms nested-attributes simple-form

8
推荐指数
2
解决办法
4166
查看次数

i18n使用simple_form标记嵌套模型

如果一个项目:

  • has_many:任务
  • accepts_nested_attributes_for:任务

我正在使用simple_form(简化):

simple_form_for @project do |f|
  f.input :project_name
  f.simple_fields_for :tasks do |j|
    j.input :task_name
  end
  f.submit
end
Run Code Online (Sandbox Code Playgroud)

我如何将标签国际化:task_name?我在我的simple_form.it.yml文件中尝试了很多组合,例如:

it:
  simple_form:
    labels:
      project:
        project_name: 'Nome progetto'
        task:
          task_name:  'Nome compito'
Run Code Online (Sandbox Code Playgroud)

无法在文档中找到示例.谷歌指出了几个明显相关的封闭问题:

https://github.com/plataformatec/simple_form/issues/48

https://github.com/plataformatec/simple_form/issues/194

但到目前为止,我感到茫然......

谢谢!朱塞佩

internationalization nested-attributes ruby-on-rails-3 simple-form

8
推荐指数
1
解决办法
6064
查看次数

在预构建嵌套模型实例时,Rails nested_form link_to_add不起作用

我正在粗略地将ryanb的令人敬畏的nested_form gem集成到我的rails 3.1.3应用程序中.我担心我的Javascript技能太有限,无法知道它是我的代码(可能)还是需要更改的宝石.也许有人可以提供帮助.

设置:我有一个":household"类:accepts_nested_attributes_for":members(people)".我正在运行开发服务器.我将nested_form.js移动到/ app/assets/javascripts目录.我几乎肯定它只被采购一次.

问题:如果在家庭控制器"新"方法中,我这样做:

@household = Household.new
Run Code Online (Sandbox Code Playgroud)

我只看到视图中的家庭原生字段(预期),并且"link_to_remove"和"link_to_add"链接呈现/删除成员字段部分(预期).但是,如果我这样做:

@household = Household.new
@household.members.build
Run Code Online (Sandbox Code Playgroud)

我在视图中看到了家庭原生字段(预期),成员本地字段的一个呈现部分(预期),但"link_to_remove"和"link_to_add"什么都不做(意外).我无法添加另一个:成员部分在那一点,也不删除已经显示的:成员部分.

我很难过.下面是看似相关的精简源文件.我从git存储库获取了nested_form插件(最后捆绑了2012.04.18)...


/app/models/household.rb

class Household < ActiveRecord::Base
  has_many :members, :class_name => "Person"
  accepts_nested_attributes_for :members
  attr_accessible :id, :name, :member_ids
  attr_accessible :members_attributes
end #class
Run Code Online (Sandbox Code Playgroud)

/app/models/person.rb

class Person < ActiveRecord::Base
  belongs_to :household  
  attr_accessible :id, :name_given, :name_middle, :name_family, :household_id 
end #class
Run Code Online (Sandbox Code Playgroud)

/app/controllers/households_controller.rb

  <snip>
  # GET /households/new
  # GET /households/new.json
  def new
    @household = Household.new
    @household.members.build     # <---- Removing this changes the behavior

    respond_to do |format|
      format.html # new.html.erb
      format.json { …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails partial-views jquery-plugins nested-attributes ruby-on-rails-3.1

8
推荐指数
1
解决办法
3627
查看次数

严重困难:没有使用fields_for触发嵌套资源的验证

我严重关注我有用户和个人资料模型的问题.无论我尝试什么,似乎都没有触发任何Profile属性的验证

  • 配置文件模型属于User.用户has_one个人资料
  • 在注册表单上使用fields_for显示一些个人资料字段(性别,城市和国家/地区)
  • 无论我尝试了什么,我都无法让性别和城市的验证工作(fields_for个人资料字段)

我的表格:

- title t(:title, :scope => :register)
%h1= yield(:title)

  = simple_form_for(resource, :as => resource_name, :html => { :class => 'form-horizontal' } , :validate => true , :url => registration_path(resource_name)) do |f|

    = f.input :username,                  :label  => t(:username)
    = f.input :email,                     :label  => t(:email),
                                          :hint   => t(:hint_email_visible)
    = f.input :password,                  :label  => t(:password), :require => true
    = f.input :password_confirmation,     :label  => t(:password_confirm)


    - resource.build_profile
    = f.fields_for :profile do |f|

      #div

        = f.hidden_field :form, :value => …
Run Code Online (Sandbox Code Playgroud)

validation activerecord ruby-on-rails nested-attributes ruby-on-rails-3

8
推荐指数
1
解决办法
983
查看次数

first_or_create通过电子邮件,然后保存嵌套模型

我两个型号UserSubmission如下:

class User < ActiveRecord::Base
  # Associations
  has_many :submissions
  accepts_nested_attributes_for :submissions

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :name, :role, :submission_ids, :quotation_ids, :submissions_attributes

  validates :email, :presence => {:message => "Please enter a valid email address" }
  validates :email, :uniqueness => { :case_sensitive => false }
end

class Submission < ActiveRecord::Base
  belongs_to :user
  attr_accessible :due_date, :text, :title, :word_count, :work_type, :rush, :user, :notes

  validates :work_type, :title, :text,:presence => true
  validates :text, :length => { …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails nested-attributes ruby-on-rails-3.2

8
推荐指数
1
解决办法
3655
查看次数

无法使用Devise批量分配受保护的属性以创建has_many嵌套模型

我已经看过RailsCast,另一个嵌套属性视频,很多SO帖子,并与此争吵了一段时间,但我仍然无法弄明白.我希望它有点小.

我有两个型号,User(通过制定创建),和Locker(又名,产品心愿),和我试图创建Locker一个User用户注册时.我的登录表单有一个字段,用于表示新的Locker(适当调用的:name)我正在尝试分配给在新用户注册时创建的锁定器.我所遇到的只有:

WARNING: Can't mass-assign protected attributes: locker

我试过的每个组合accepts_nested_attributesattr_accesible在我的两个车型,但仍然没有什么作品.我可以从日志中看到它正在由Devise #create方法处理,我知道Devise不够聪明,无法创建我想要的模型:)

这是我的两个模型的相关部分:

# user.rb    
class User < ActiveRecord::Base
  attr_accessible :username, :email, :password, :password_confirmation, :remember_me, :locker_attributes

  # Associations
  has_many :lockers
  has_many :lockups, :through => :lockers

  # Model nesting access
  accepts_nested_attributes_for :lockers
end
Run Code Online (Sandbox Code Playgroud)

# locker.rb
class Locker < ActiveRecord::Base
  belongs_to :user
  has_many :lockups
  has_many :products, :through => :lockups 

  attr_accessible :name, :description
end

# lockers_controller.rb (create)
    @locker …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails mass-assignment nested-attributes devise ruby-on-rails-3

8
推荐指数
1
解决办法
3007
查看次数