所以我正在使用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
如何为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
我有一个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帖子中下拉列表的选定项的值.
谢谢,皮特
我的目标是使用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) 我正在尝试使用带有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)
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
更新:我正在尝试将表单字段添加/删除到涉及多个模型的嵌套表单.我已经看过Ryan Bates的"动态表格"轨道广播,我已经使用Cocoon Gem参考了这篇文章.在该文章之后,除了child_index之外,一切都完美无缺.child_index仅出现在第一个输入字段()和第一个输入字段(和)上.然后它返回到正在添加的字段的真实性标记.:kid:name:pet:name:age
我已经删除了所有的JS和帮助器方法,而是使用了一些内置JS的Cocoon方法.
我修复了问题,单击"添加"将添加两个字段而不是= javascript_include_tag :cocoon从application.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) 在我的应用程序中,用户有很多项目.我想创建一个" 添加多个项目 "表单,因此用户可以一次创建多个项目.
在我看来,最快的方法是在其中嵌套项目字段的用户表单,并省略用户字段.这样,当提交表单时,将保存用户并自动创建所有新的项目记录.
但是,我不希望现有的项目显示在表单中.只有正在创建的新项目的空字段(来自@ 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的复杂形式示例.代码工作正常.我只是想省略现有项目以这种形式出现.
有没有办法按类型删除嵌套列表中的项目(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) 我的用户模型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) 考虑一下这段代码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稳定
nested-forms ×10
simple-form ×2
actionview ×1
ajax ×1
clojure ×1
file-upload ×1
form-submit ×1
forms ×1
has-many ×1
html-table ×1
jquery ×1
node.js ×1
paperclip ×1
reactjs ×1
ruby ×1