标签: nested-form-for

Rails 4不更新嵌套属性

问题:当我点击关联的操作时,它们是在现有嵌套属性的基础上创建的,而不是更新嵌套属性#updatefeatures_controller.rb

可能的原因:我认为问题在于我在Rails中缺乏理解form_for.我认为细分在我的视图中,如何呈现持久的嵌套属性,和/或我如何指定嵌套属性的id失败,导致它只是创建一个新的id

feature.rb

class Feature < ActiveRecord::Base
  ...
  has_many :scenarios
  accepts_nested_attributes_for :scenarios,
    allow_destroy: true,
    reject_if: :all_blank
  ...
end
Run Code Online (Sandbox Code Playgroud)

features_controller.rb

def update
  ...
  project = Project.find(params[:project_id])
  @feature = Feature.find(params[:id])

  if @feature.update_attributes(feature_params)
    # checking feature_params looks good...
    # feature_params['scenarios'] => { <correct object hash> }

    redirect_to project
  else
    render :edit
  end
end

...

private
def feature_params
  params.require(:feature).permit(:title, :narrative, :price, :eta, scenarios_attributes[:description, :_destroy])
end
Run Code Online (Sandbox Code Playgroud)

_form.html.haml(简体)

= form_for [@project, @feature] do |f|
  ... …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-attributes nested-form-for ruby-on-rails-4

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

Rails 4:fields_for中的fields_for

我正在学习RoR,我正在尝试找到如何使用has_one模型在另一个中设置fields_for:

class Child < ActiveRecord::Base
    belongs_to :father
    accepts_nested_attributes_for :father
end

class Father < ActiveRecord::Base
    has_one :child
    belongs_to :grandfather
    accepts_nested_attributes_for :grandfather
end


class Grandfather < ActiveRecord::Base
    has_one :father
end
Run Code Online (Sandbox Code Playgroud)

我在Railscasts上使用嵌套模型表单第1部分来获取这些:在children_controller.rb中:

  def new
    @child = Child.new
    father=@child.build_father
    father.build_grandfather
  end

def child_params
      params.require(:child).permit(:name, father_attributes:[:name], grandfather_attributes:[:name])
    end
Run Code Online (Sandbox Code Playgroud)

我的形式:

<%= form_for(@child) do |f| %>
  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  mother:<br>
  <%= f.fields_for :father do |ff| %>
    <%= ff.label :name %>
    <%= ff.text_field :name %><br>
      grand mother:<br>
      <%= f.fields_for :grandfather …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails form-for fields-for nested-form-for ruby-on-rails-4

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

无法使用nested_form_for gem

我是第一次使用nested_form_for gem.我不知道这是一个问题,或者我使用它是错误的,但我得到一个"未定义的方法nested_form_for"错误.我有一个非常规则的表格,你可以看到:

  <%= nested_form_for @user do |f| %>
        <%= f.fields_for :godfathers do |godfather_form| %>
            <%=  godfather_form.label :name %> <br/>
            <%=  godfather_form.text_field :name %> <br/>
            <%=  godfather_form.label :description %> <br/>
            <%=  godfather_form.text_field :description %> <br/>
            <%=  godfather_form.link_to_remove "Remove this godfather" %>
        <% end %>
       <%= f.link_to_add "Add a godfather", :godfathers %>
   <% end %>
Run Code Online (Sandbox Code Playgroud)

顺便说一下,我安装了gem并运行:rails generate nested_form:install command生成我在jquery includes(<%= javascript_include_tag :default, 'nested_form' %>)之后包含在我的布局中的nested_form.js文件.

谁也使用这个宝石?

谢谢!

ruby-on-rails-3 nested-form-for

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

form_for [@nested,@ resource],remote =>使用format.html rails 3.2.6进行回复

无法获得ajax form_for以尊重format.js respond_to仅响应format.html.任何帮助非常感谢.

视图

此视图由AJAX部分调用,然后用户提交表单.初始的ajax调用会混淆这个表单的'remote:true'吗?

<%= form_for([@nested, @nested.resources.new], remote: true) do |i|%>
  <%= i.hidden_field :inviter_id, value: current_user.id %>
  <%= i.hidden_field :fb_pic_url, value: f['pic_square'] %>
  <%= i.hidden_field :name, value: f['name'] %>
  <%= i.hidden_field :uid, value: f['uid'] %>
  <%= i.submit "Invite", class:"btn btn-success invite_button" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

的routes.rb

resources :nested do 
  resources :resources
end
Run Code Online (Sandbox Code Playgroud)

调节器

def create
  code code code

  respond_to do |format|
    format.html { redirect_to @nested, notice: "Successfully Posted Nested" }
    format.json { render json: @nested, status: :created, location: @nested …
Run Code Online (Sandbox Code Playgroud)

ajax ruby-on-rails form-for nested-form-for ruby-on-rails-3.2

7
推荐指数
1
解决办法
1785
查看次数

Rails 3:nested_form,collection_select,accepts_nested_attributes_for和fields_for

更新:在这里回答.

这里有很多很好的问题和答案,关于获取nested_form,collection_select,accepts_nested_attributes_for和fields_for可以很好地一起玩的互联网,但我仍然难过.如果你能帮助我,请提前致谢.

目标:生成新的isbn记录.isbn可以有很多贡献者.我成功地使用ryanb nested_form gem根据需要在表单上动态生成新的贡献者字段.其中一个字段使用Contributor中的所有名称记录的collection_select下拉列表.创建新记录时,需要将许多贡献者ID写入连接表(contributors_isbns).

我有一些工作,但只是我可以将一个贡献者ID保存到isbns表中的新记录.我似乎无法将任何数据写入连接表.

我有三个型号.贡献者和Isbns有很多关系,我用has_many做过:通过.isbn可以拥有许多贡献者,贡献者可以拥有许多isbns.他们通过contributors_isbn加入.

isbn.rb

  attr_accessible               :contributor_id
  has_many                      :contributors, :through => :contributors_isbns
  has_many                      :contributors_isbns
  accepts_nested_attributes_for :contributors
  accepts_nested_attributes_for :contributors_isbns
Run Code Online (Sandbox Code Playgroud)

contributor.rb

  attr_accessible               :isbn_id
  has_many                      :contributors_isbns
  has_many                      :isbns, :through => :contributors_isbns
  accepts_nested_attributes_for :isbns
Run Code Online (Sandbox Code Playgroud)

contributors_isbn.rb

  class ContributorsIsbn
  attr_accessible               :isbn_id, :contributor_id
  belongs_to                    :isbn
  belongs_to                    :contributor
  accepts_nested_attributes_for :contributors
Run Code Online (Sandbox Code Playgroud)

在isbns控制器中:

 def new
    @isbn  = Isbn.new
    @title = "Create new ISBN"
    1.times {@isbn.contributors.build}
    @isbn.contributors_isbns.build.build_contributor
  end
Run Code Online (Sandbox Code Playgroud)

(显然我无法理解使用哪种构建方法.)

在isbns new.html.erb视图中:

<%= nested_form_for @isbn, :validate => false do |f| %>
<h1>Create new ISBN</h1>
<%= render …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails fields-for ruby-on-rails-3 nested-form-for

6
推荐指数
1
解决办法
4581
查看次数

动态嵌套表单link_to_add调用两次

我正在使用ryan bates nested_form gem来动态地向表单添加一些嵌套字段.

例如

<%= f.fields_for :phones do |phone_form| %>
<%= phone_form.text_field :phone_number %>
<% end %>
<%= f.link_to_add "Add a phone", :phones %></p>
Run Code Online (Sandbox Code Playgroud)

一切正常,但每次点击链接时都会添加两个空字段.

我放了一个断点 $('form a.add_nested_fields').live('click', function() ,看到它被叫了两次......

我在mac上使用chrome

javascript jquery ruby-on-rails nested-forms nested-form-for

6
推荐指数
1
解决办法
2290
查看次数

嵌套字段与mongodb

我正在尝试使用mongodb创建嵌套字段.为此,我正在使用gem mongomodel,它允许使用ruby和mongodb并且我正在使用gem nested_form来创建动态嵌套字段.我遇到了以下麻烦:

undefined method##的reflect_on_association'

我在互联网上发现的其他类似错误与我想用mongodb做的​​事情并不匹配.我是RoR的新手,我不知道如何解决这个问题.谁能帮我?

这是我的模特:

survey.rb

class Survey < MongoModel::Document
  property :name, String
  property :questions, Collection[Question]
  timestamps!

  accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end
Run Code Online (Sandbox Code Playgroud)

questions.rb

class Question < MongoModel::Document
  property :content, String
  timestamps!
end
Run Code Online (Sandbox Code Playgroud)

我的控制器:

surveys_controller.rb
class SurveysController < ApplicationController
  # GET /surveys
  # GET /surveys.json
  def index
    @surveys = Survey.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @surveys }
    end
  end

  # GET /surveys/1
  # …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails nested-forms mongodb nested-form-for

6
推荐指数
1
解决办法
729
查看次数

Rails 3.1没有从nested_form渲染fields_for

我有一个项目模型has_many :tasks.我在rails 3.1中添加了一个嵌套资源,现在尝试使用nested_form gem来编辑项目时添加/删除任务.

我之前在另一个Rails3应用程序中使用了nested_form,它工作正常,但现在我的fields_for部分没有呈现任何东西.

这是我的代码:

#project.rb
class Project < ActiveRecord::Base

  attr_accessible :nr, :name, :client_id, :project_status_id, :hidden, :active, :description, :tasks_attributes


  has_many :tasks, :dependent => :destroy
  accepts_nested_attributes_for :tasks, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true


end


#task.rb
class Task < ActiveRecord::Base

  belongs_to :project

end

#views/projects/_form.html.erb
<%= simple_nested_form_for @project do |form| %>

  <%= form.input :nr, :label => 'Nr' %>
  <%= form.input :name, :label => 'Name' %>
  <%= form.association :client, :collection => Client.all(:order => 'name'), …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-forms nested-form-for simple-form ruby-on-rails-3.1

5
推荐指数
1
解决办法
3231
查看次数

form_for包含模块和命名空间

我在财务模块中有以下模型:

class Finance::BillRec < ActiveRecord::Base
  ...
  has_many :bill_rec_offs, :dependent => :destroy
  ...
end

class Finance::BillRecOff < ActiveRecord::Base
  ...
  belongs_to :bill_rec
  ...
end
Run Code Online (Sandbox Code Playgroud)

我在我的form_for上这样做:

<%= form_for([@bill_rec, @bill_rec_off]) do |f| %>
  ...
<% end %>
Run Code Online (Sandbox Code Playgroud)

的routes.rb

namespace :finance do
  resources :bill_recs do
    resources :bill_rec_offs
  end
end
Run Code Online (Sandbox Code Playgroud)

而错误:

undefined method `finance_bill_rec_finance_bill_rec_offs_path' for #<#<Class:0x000000070757e0>:0x0000000708bec8>
Run Code Online (Sandbox Code Playgroud)

但是,路线finance_bill_rec_bill_rec_off_path(@bill_rec_off)效果很好.

如何在带有命名空间的form_for和带有模块的嵌套路由上执行此操作?

ruby-on-rails form-for nested-form-for

4
推荐指数
1
解决办法
2360
查看次数

如何使用simple_form和嵌套表单通过关联为has_many创建嵌套表单?

我有一个rails应用程序,有一个专辑和歌曲模型,有很多关系.我正在尝试使用simple_formnested_form gems 将歌曲添加到相册.

如果我使用simple_form,则很容易创建关联,但是我无法使用nested_form.看来这应该有效:

<%= f.fields_for :songs do |song_form| %>
  <%= song_form.association :songs %>
  <%= song_form.link_to_remove "Remove this song" %>
<% end %>
<p><%= f.link_to_add "Add a song", :songs %></p>
Run Code Online (Sandbox Code Playgroud)

但是我得到了这个错误:RuntimeError in Albums#new Association :songs not found.如果我只使用simple_form,该关联工作正常.

正确的语法是什么?或者这些宝石不兼容?如果两个宝石不兼容,那么如何使用nested_form添加和删除相册中的歌曲?

/ views/albums/_form https://gist.github.com/leemcalilly/51e7c5c7e6c4788ad000

/ models/album https://gist.github.com/leemcalilly/9a16f43106c788ab6877

/ models/song https://gist.github.com/leemcalilly/0ccd29f234f6722311a0

/ models/albumization https://gist.github.com/leemcalilly/c627ad2b178e1e11d637

/ controllers/albums_controller https://gist.github.com/leemcalilly/04edf397b2fb2a3d0d1d

/ controllers/songs_controller https://gist.github.com/leemcalilly/bcbccc9259c39d0b6b7a

ruby-on-rails nested-forms has-many-through nested-form-for simple-form

4
推荐指数
1
解决办法
2637
查看次数

Rails 5 - 保存回滚,因为嵌套模型父模型没有在子模型之前保存

好的伙计们,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

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

为什么 Rails 中的强参数顺序很重要?

在我的product_model_controller.rb强参数中,我有以下代码:

  def product_model_params
    params.require(:product_model)
          .permit(:name, :product_category_id, 
                  product_category_attributes: [:id, :name], attr_val_ids: [])
  end
Run Code Online (Sandbox Code Playgroud)

按照它的方式,它工作得很好。但是,如果我更改参数的顺序,它就会停止工作。例子:

  def product_model_params
    params.require(:product_model)
          .permit(:name, product_category_attributes: [:id, :name],
                  :product_category_id, attr_val_ids: [])
  end
Run Code Online (Sandbox Code Playgroud)

错误:

语法错误,意外的 ',',期望 => ..., :name], :product_category_id, attr_val_ids: []) ... ^

为什么会出现这种情况?我已经坚持了很长时间了:/


产品模型.rb

class ProductModel < ApplicationRecord
  validates :name, presence: true
  validates :name, uniqueness: true

  has_many :products
  has_many :product_model_attr_vals
  has_many :attr_vals, through: :product_model_attr_vals
  has_many :attrs, through: :attr_vals

  belongs_to :product_category

  accepts_nested_attributes_for :product_model_attr_vals
  accepts_nested_attributes_for :product_category
end
Run Code Online (Sandbox Code Playgroud)

产品类别.rb

class ProductCategory < ApplicationRecord
  validates :name, presence: true
  validates …
Run Code Online (Sandbox Code Playgroud)

forms ruby-on-rails nested-forms nested-form-for strong-parameters

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

如何访问嵌套属性的验证错误?

<%= form_for @article , :html => {:multipart=> true} do |f| %>
  <% if @article.errors.any? %>
    <ul>
      <% @article.errors.full_messages.each do |msg| %>
       <li><%= msg %></li>
      <% end %>
    </ul>
  <% end %>
Run Code Online (Sandbox Code Playgroud)

上面是我的表单的一个片段,我可以访问文章的验证,即:author, :title 的validates_presence 但是我无法访问我为nested_attributes 设置的验证,这些验证恰好是照片。关于如何显示错误消息的任何想法?

ruby-on-rails nested-attributes nested-form-for

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