标签: nested-forms

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
查看次数

通过与其他属性相关联来创建has_many的Rails表单?

如何为has_many :through具有其他属性的关联生成表单字段?

has_many :through关系有一个名为的附加列weight.

这是连接表的迁移文件:

create_table :users_widgets do |t|
  t.integer :user_id
  t.integer :widget_id
  t.integer :weight

  t.timestamps
end
Run Code Online (Sandbox Code Playgroud)

模型看起来像这样:

User
  has_many :widgets, :through => :users_widgets,
           :class_name => 'Widget',
           :source => :widget
  has_many :users_widgets
  accepts_nested_attributes_for :widgets # not sure if this is necessary


Widget
  has_many :users, :through => :users_widgets,
           :class_name => 'User',
           :source => :user
  has_many :users_widgets
  accepts_nested_attributes_for :users # not sure if this is necessary


UsersWidget
  belongs_to :user
  belongs_to :widget
Run Code Online (Sandbox Code Playgroud)

为了简单起见,Widget和User只有一个自己的字段叫做nameergo User.first.name和 …

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

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

ASP .NET MVC 3 - 如何提交嵌套在html表单中的ajax表单

我有一个ASP .NET MVC 3项目和我的一个"创建"视图的问题.

我有使用ajax表单实现的级联下拉字段.

粗略地说这个观点 - 像这样:

@using (Html.BeginForm(...))
{
    @Html.MyDropDown1

    using (Ajax.BeginForm(...))
    {
        @Ajax.MyDropdown2
        <input type="submit" value="Select" />
    }

    using (Ajax.BeginForm(...))
    {
        @Ajax.MyDropdown3
        <input type="submit" value="Select" />
    }

    <!-- other form fields -->

    <input type="submit" value="Create" />
}
Run Code Online (Sandbox Code Playgroud)

问题是ajax表单中的提交按钮实际上提交了外部html表单.

有没有办法指定我要提交的表单的名称?

我想把我的ajax表单放在我的html表单上面所以不会有任何嵌套 - 但我需要在我的html帖子中下拉列表的选定项的值.

谢谢,皮特

ajax form-submit nested-forms asp.net-mvc-3

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

Rails nested_form将项添加到表行

我的目标是使用nested_form gem:https://github.com/ryanb/nested_form, 但是每次添加对象时,我都不想创建一组新的标签和字段,而是想在现有表中插入一行.

= nested_form_for @transaction do |f|
%h3 Line Items 
%table
  %tr
    %th Branch
    %th Department
    %th Invoice #
    %th Amount
    %th Transaction Type
    %th Deposit (y/n)
    %th

  = f.fields_for :line_items do |line_item|
    %tr
      %td 
        = line_item.text_field :location_id
      %td 
        = line_item.text_field :department_id
      %td 
        = line_item.text_field :invoice_num
      %td 
        = line_item.text_field :amount
      %td 
        = line_item.text_field :transaction_type
      %td 
        = line_item.text_field :deposit
      %td= line_item.link_to_remove "Remove"
    %p= f.link_to_add "Add", :line_items
Run Code Online (Sandbox Code Playgroud)

.link_to_add按钮只在第一行创建了一堆字段,第一行是td.

<h3>Line Items</h3>
<table>
  <tr>
    <th>Branch</th>
    <th>Department</th>
    <th>Invoice #</th>
    <th>Amount</th>
    <th>Transaction …
Run Code Online (Sandbox Code Playgroud)

html-table nested-forms ruby-on-rails-3

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

Rails:带嵌套和强力参数的照片上传(回形针)

背景:

我正在尝试使用带有Paperclip gem的Strong Params将照片上传添加到广告模型,其中Photo是一个单独的模型"has_attached_file":upload,我在调用forms_for(实际上是semantic_forms_for,因为我正在使用Formtastic)广告#new视图将图像上传到Photo实例.Params看起来不错,但我想我在控制器中遗漏了一些东西.尽管多次迭代控制器代码,但仍无法使其工作.我需要做些什么才能让它发挥作用?

非常感谢任何提示或指示!谢谢

-

广告模型:

class Ad < ActiveRecord::Base
  belongs_to :user

    ##edited out irrelevant associations

  has_many :photos, dependent: :destroy
  accepts_nested_attributes_for :photos, :allow_destroy => true
  default_scope { order("created_at DESC") }
  #validates_presence_of :user_id, :make, :model, :km, :year, :location
end
Run Code Online (Sandbox Code Playgroud)

-

照片型号:

 class Photo < ActiveRecord::Base
      belongs_to :ad
      has_attached_file :upload, :styles => { :main => "600x500>", 
                                                                                    :thumb => "60x45>" }, 
      :default_url => "http://placehold.it/600x500&text=nice+wheels!"
    end
Run Code Online (Sandbox Code Playgroud)

AdsController

class AdsController < ApplicationController

    def create
        @ad = Ad.new(ad_params)
      @ad.user_id = current_user.id
      @photo = @ad.photos.build
        if …
Run Code Online (Sandbox Code Playgroud)

file-upload ruby-on-rails paperclip nested-forms strong-parameters

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

Rails 4:使用Cocoon Gem将child_index添加到动态添加(嵌套)表单字段

更新:我正在尝试将表单字段添加/删除到涉及多个模型的嵌套表单.我已经看过Ryan Bates的"动态表格"轨道广播,我已经使用Cocoon Gem参考了这篇文章.在该文章之后,除了child_index之外,一切都完美无缺.child_index仅出现在第一个输入字段()和第一个输入字段(和)上.然后它返回到正在添加的字段的真实性标记.:kid:name:pet:name:age

我已经删除了所有的JS和帮助器方法,而是使用了一些内置JS的Cocoon方法.

我修复了问题,单击"添加"将添加两个字段而不是= javascript_include_tag :cocoonapplication.html.erb文件中删除一个字段.

我试过添加jQuery和表单助手,但我不确定我是否正确输入了代码.

(我更改了模型对象以使关系更清晰)

parent.rb文件:

class Parent < ActiveRecord::Base

has_many :kids
has_many :pets, through: :kids # <<<<<< ADDED KIDS USING 'through:'
Run Code Online (Sandbox Code Playgroud)

kid.rb文件:

class Kid < ActiveRecord::Base

belongs_to :parent
has_many :pets
accepts_nested_attributes_for :pets, reject_if: :all_blank, allow_destroy: true
validates :name, presence: true
Run Code Online (Sandbox Code Playgroud)

pet.rb文件:

 class Pet < ActiveRecord::Base

 belongs_to :kid

 validates :name, presence: true

 validates :age, presence: true
Run Code Online (Sandbox Code Playgroud)

这是我的_form.html.erb文件:

 <%= form_for @parent do |f| %> …
Run Code Online (Sandbox Code Playgroud)

forms jquery ruby-on-rails nested-forms

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

如何在rails中以嵌套形式省略现有的子记录?

在我的应用程序中,用户有很多项目.我想创建一个" 添加多个项目 "表单,因此用户可以一次创建多个项目.

在我看来,最快的方法是在其中嵌套项目字段的用户表单,并省略用户字段.这样,当提交表单时,将保存用户并自动创建所有新的项目记录.

但是,我不希望现有的项目显示在表单中.只有正在创建的新项目的空字段(来自@ user.projects.build).是否有我可以传递的参数或我可以在表单中更改的内容以省略现有的Project记录?

<% form_for (@user) do |f| %>

   <% f.fields_for :project do |project_form| %>
      <%= render :partial => 'project', :locals => {:f => project_form}  %>
   <% end %>

   <%= add_child_link "New Project", f, :projects %>

   <%= f.submit "save" %> 

<%end%>
Run Code Online (Sandbox Code Playgroud)

我正在使用Ryan Bate的复杂形式示例.代码工作正常.我只是想省略现有项目以这种形式出现.

ruby-on-rails nested-forms nested-attributes

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

如何从Clojure中的嵌套列表或向量中按类型删除项?

有没有办法按类型删除嵌套列表中的项目(1 [2] 3(4 [5] 6))变为(1 3(4 6))如果我只想删除向量?

使用postwalk,我可以用nil替换所有向量,但我找不到删除它们的方法.

(clojure.walk/postwalk 
  #(if (vector? %) nil %) '(1 [2] 3 (4 [5] 6)))

=>(1 nil 3 (4 nil 6))
Run Code Online (Sandbox Code Playgroud)

clojure nested-forms

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

Rails嵌套表单不能保存孩子

我的用户模型has_many响应.我正在尝试创建一个嵌套表单来创建一个具有三个子响应的新用户.我的代码看起来与Rails强制转换相同,但是虽然它会保存用户,但它不会保存它们的响应.任何人都可以看到有什么问题?

users_controller.rb

class UsersController < ApplicationController
  def new
    @user = User.new
    3.times{@user.responses.build}
    @responses = @user.responses
  end

  def create
    @user = User.new(user_params)
    if @user.save
      redirect_to @user #todo: where do we want to redirect?
    else
      render 'new'
    end
  end

  def show
    @user = User.find(params[:id])
    @responses = @user.responses
  end

  def index
    @users = User.all
  end

  private

    def user_params
      params.require(:user).permit(:username, :email, :responses)
    end
end
Run Code Online (Sandbox Code Playgroud)

user.rb

class User < ActiveRecord::Base
  attr_accessor :responses_attributes
  has_many :responses, :dependent => :destroy
  accepts_nested_attributes_for :responses#, :reject_if => lambda { |a| a[:content].blank? …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails has-many nested-forms

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

如何在ReactJS中使用嵌套表单?

考虑一下这段代码snnipet:

<%= form_for @survey do |f| %>
  <%= f.error_messages %>
 <p>
   <%= f.label :name %><br />
   <%= f.text_field :name %>
 </p>
  <%= f.fields_for :questions do |builder| %>
    <%= render "question_fields", :f => builder %>
  <% end %>
 <p><%= f.submit "Submit" %></p>
<% end %>
Run Code Online (Sandbox Code Playgroud)

它会返回我有一些哈希(rails dev会知道).我的问题是如何在ReactJS中制作这种格式?接收与Rails默认完全相同的参数.至于现在,我只能在JSX中使用HTML标签.目前我在控制器中接收params并根据需要对它们进行排序(这看起来很糟糕).我已经测试了一些npm软件包,但是他们的文档似乎没有帮助!其中包括:https: //www.npmjs.com/package/react-rails-form-helpers

https://www.npmjs.com/package/react-form

这有什么npm包吗?使用react_on_rails gem和Rails 5稳定

ruby-on-rails nested-forms node.js reactjs react-on-rails

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