我正在使用nested_attributes并尝试在Ryan Bates关于嵌套模型的屏幕录像(#196)之后实时添加/删除字段ajax
这样做,它将无法工作,但删除"link_to_add_fields"行时,它可以正常工作.
问题是,我不知道我是否正确地做了这一切.
<%= form_for(@item) do |f| %>
<% if @item.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@item.errors.count, "error") %> prohibited this item from being saved:</h2>
<ul>
<% @item.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :item_type_id %><br />
<%= f.select :item_type_id, @item_types.collect { |p| [ p.title, p.id ]} %>
<br />
<%= f.fields_for :item_parts do |parts_form| %> …Run Code Online (Sandbox Code Playgroud) 我不确定嵌入式类的控制器应该怎么做.传递的参数确实显示了所有嵌入的类属性,包括图像属性,但只有非图像参数才会保存在数据库中.这告诉我问题不在于ORM的选择(在这种情况下是Mongoid),而是与我使用载波的方式有关:
Parameters: {"article"=>{"name"=>"New article", "comments_attributes"=>{"0"=>{"remote_image_url"=>"", "name"=>"Comment 1", "content"=>"comment content....", "image"=>#<ActionDispatch::Http::UploadedFile:0x10339d880 @headers="Content-Disposition: form-data; name=\"article[comments_attributes][0][image]\"; filename=\"dh.png\"\r\nContent-Type: image/png\r\n", @original_filename="dh.png", @tempfile=#<File:/var/folders/A1/A1SUPUTUFA8BYB5j+RD2L++++TI/-Tmp-/RackMultipart20120228-21178-1vckii1-0>, @content_type="image/png">}}, "content"=>"article content"}, "commit"=>"Create Article", "authenticity_token"=>"i14YuJs4EVKr5PSEw9IwKXcTbQfOP4mjbR95C75J2mc=", "utf8"=>"\342\234\223"}
MONGODB (89ms) freedb['system.namespaces'].find({})
MONGODB (0ms) freedb['articles'].insert([{"name"=>"New article", "comments"=>[{"name"=>"Comment 1", "_id"=>BSON::ObjectId('4f4daf6a58001652ba000012'), "content"=>"comment content...."}], "_id"=>BSON::ObjectId('4f4daf6958001652ba000011'), "content"=>"article content"}])
Run Code Online (Sandbox Code Playgroud)
父模型:
class Article
include Mongoid::Document
field :name, :type => String
field :content, :type => String
embeds_many :comments
accepts_nested_attributes_for :comments
end
Run Code Online (Sandbox Code Playgroud)
儿童模特:
require 'carrierwave/mongoid'
class Comment
include Mongoid::Document
field :name, :type => String
field :content, :type => String
field :image, :required => …Run Code Online (Sandbox Code Playgroud) 我有这样的嵌套资源
resources :profiles do
resources :albums do
resources :images
end
end
match ':username' => "profiles#show", :as => 'profile'
Run Code Online (Sandbox Code Playgroud)
例如,特定图像的网址是
http://localhost:3000/profiles/Azzurrio/albums/4/images/1
Run Code Online (Sandbox Code Playgroud)
我不能在我的模板中使用配置文件用户名,当我使用params [:username]时它不起作用,所以任何人都可以告诉我如何处理这个参数?
我正在使用simple_form,我只想使用分类表创建类别和文章之间的关联.
但我有这个错误:无法批量分配受保护的属性:category_ids.app/controllers/articles_controller.rb:36:在`update'中
articles_controller.rb
def update
@article = Article.find(params[:id])
if @article.update_attributes(params[:article]) ---line with the problem
flash[:success] = "?????? ?????????"
redirect_to @article
else
render :edit
end
end
Run Code Online (Sandbox Code Playgroud)
article.rb
has_many :categorizations
has_many :categories, through: :categorizations
Run Code Online (Sandbox Code Playgroud)
category.rb
has_many :categorizations
has_many :articles, through: :categorizations
Run Code Online (Sandbox Code Playgroud)
categorization.rb
belongs_to :article
belongs_to :category
Run Code Online (Sandbox Code Playgroud)
分类有article_id和category_id字段.
我的_form.html.erb
<%= simple_form_for @article, html: { class: "form-horizontal", multipart: true } do |f| %>
<%= f.error_notification %>
<%= f.input :title %>
<%= f.association :categories %>
<%= f.input :teaser %>
<%= f.input :body %>
<%= f.input …Run Code Online (Sandbox Code Playgroud) ruby-on-rails associations nested-forms ruby-on-rails-3 simple-form
我正在尝试创建一个由多个嵌套的可排序表单组成的导航菜单编辑器,这些表单最终将作为一个包含所有表单数据的巨型嵌套JSON数据blob集中提交.
这两个库,我检查是nestableSortable(https://github.com/mjsarfatti/nestedSortable)和嵌套(https://github.com/dbushell/Nestable).这两个库似乎是这类工作中最常用的项目,但似乎都没有内置的功能来序列化和嵌套这些表单.现在我假设任何一个库都要求我以某种方式自己构建这个功能.
Nestable似乎有点新,并且不依赖于JQuery-UI,而nestableSortable似乎更富有功能.我也想知道是否有人有嵌套表单的类似问题,以及他们是否有任何关于如何使这些库轻松地将嵌套表单序列化为JSON结构的提示,以及这些库中的任何一个是否更适合这样的一个专案.
在我的rails 4 app中,我有一个三重嵌套路线:
devise_for :users do
resources :foo do
resources :marflar
end
end
Run Code Online (Sandbox Code Playgroud)
我有一个用于创建带有嵌入式Marflar对象的新Foo的表单:
<%= form_for(@foo) do |f| %>
<%= f.text_field :foo_attr %>
<%= f.fields_for :marflars_attributes do |marflars_form| %>
<%= marflars_form.text_field :marflar_attr %>
<% end %>
<%= f.submit %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
但是当我提交表格时,我得到:
TypeError in FoosController#create
no implicit conversion of Symbol into Integer
Run Code Online (Sandbox Code Playgroud)
我的Foo控制器的相关部分如下所示:
def new
@foo = current_user.foos.build
@foo.marflars.build
end
def create
@foo = Foo.new(foo_params)
if @foo.save
redirect_to @foo
else
render action: 'new'
end
end
..
def foo_params
params.require(:foo).permit(:foo_attr, …Run Code Online (Sandbox Code Playgroud) 我更喜欢在R中使用列表.现在我想创建几个嵌套列表.我已经知道列表应包含的元素数量,现在想在将数据输入到列表之前定义列表的大小.对于最高级别的列表,其中包含我使用的2个元素
list_example <- vector(mode="list", 2)
Run Code Online (Sandbox Code Playgroud)
现在两个元素中的每一个都应该包含3个元素:再次包含列表的列表等等...总共我有7个级别,即2*3*3*4*2*3*3组合的树.有一种简洁的方法来定义这种深层嵌套列表结构的大小吗?非常感谢你提前!!
我有一个属于个人资料模型的工作模型.配置文件has_many工作.我有一个嵌套的模型表单,用户可以在其中添加作业.表单已成功添加作业,但编辑/更新无效.相反,当我尝试编辑作业时,它会保留旧版本的作业,但也会添加新版本.它不会用新版本替换旧版本.我该如何解决?我很确定它与编辑/更新控制器有关.
编辑控制器:
def edit
@profile = current_user.profile
end
Run Code Online (Sandbox Code Playgroud)
更新控制器:
def update
#if current_user.profile.jobs.any?
@profile = current_user.profile.update_attributes(profile_params)
if current_user.profile.invalid?
@profile = current_user.profile
render :edit, :status => :unprocessable_entity
else
redirect_to profile_path(current_user.profile_name)
end
end
Run Code Online (Sandbox Code Playgroud)
问题是,更新控制器正在处理非嵌套信息,它不适用于嵌套作业.以下是强大的参数:
def profile_params
params.require(:profile).permit(:title,
:category, :description, :state, :zip_code, :rate,
jobs_attributes: [:firm, :position, :category, :description,
:begin, :end, :_destroy])
end
Run Code Online (Sandbox Code Playgroud)
这是配置文件模型:
class Profile < ActiveRecord::Base
belongs_to :user
has_many :jobs
accepts_nested_attributes_for :jobs , :reject_if => :all_blank, :allow_destroy => true
end
Run Code Online (Sandbox Code Playgroud)
此外,这是我的路线,如果这将有所帮助:
resources :profiles do
resources :jobs
end
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助.
编辑
以下是创建动作中的参数:
{"jobs_attributes"=>{"1383593195849"=>{"firm"=>"1st firm", …Run Code Online (Sandbox Code Playgroud) ruby-on-rails nested-forms nested-attributes ruby-on-rails-4 rails-activerecord
我正在关注RailsCast#196 Nested Model Form Part 1.我给出了控制器和模型的相同名称,它是所有属性.但现在当我尝试进行编辑并删除问题时.如果我选中该复选框,则不会删除该问题.
像这样:
模型:
class Survey < ActiveRecord::Base
has_many :questions, :dependent => :destroy
accepts_nested_attributes_for :questions, :reject_if => lambda {|a| a[:content].blank?} , :allow_destroy => true
end
class Question < ActiveRecord::Base
belongs_to :survey
end
Run Code Online (Sandbox Code Playgroud)
调查控制员:
class SurveysController < ApplicationController
before_action :set_survey, only: [:show, :edit, :update, :destroy]
def index
@surveys = Survey.all
end
def show
end
def new
@survey = Survey.new
3.times {@survey.questions.build}
end
def edit
end
def create
@survey = Survey.new(survey_params)
respond_to do |format|
if @survey.save …Run Code Online (Sandbox Code Playgroud) 我有一系列表单(每个表单由1个组件管理).
这些表单中有一种输入模式(例如,其中许多都需要允许输入地址),我必须重构为可重用的组件,因为它们以多种形式使用,我不想复制它们的逻辑也不是他们的模板
每个可重用的组件都必须这样做
<form>标签address: {street: "...", "city": "...", ...})从本教程为Angular2,我在了解如何实现目标1,2和4.
本教程中的解决方案还允许实现其他目标,但它通过从父级执行所有操作来实现(请参阅参考资料app.component.ts#initAddress).
我怎样才能实现3,5,6和7,而孩子中声明控件和它们的约束?
nested-forms angular-validation angular angular-forms angular5
nested-forms ×10
associations ×2
ruby ×2
angular ×1
angular5 ×1
carrierwave ×1
javascript ×1
jquery ×1
json ×1
mongoid ×1
mptt ×1
r ×1
railscasts ×1
restful-url ×1
simple-form ×1