Rails 5没有使用Cocoon gem在Ruby

编程入门 行业动态 更新时间:2024-10-28 00:14:14
Rails 5没有使用Cocoon gem在Ruby-on-Rails中使用嵌套窗体保存到我的表中(Rails 5 not saving to my table using nested forms in Ruby-on-Rails using Cocoon gem)

RAILS 5问题。 我有一个项目,我希望用户可以选择向表单添加额外的项目。 我的代码正确呈现,并且添加更多字段的链接确实会添加更多文本字段。 我遇到的问题是没有保存到我的数据库。 我在MySQl数据库上使用mysql2 gem。 我发布了控制台输出。 这是我拥有的:

forms_controller

# forms_controller.rb def new @form = Form.new respond_to do |format| format.html # new.html.erb format.xml { render :xml => @form } end end def create @form = Form.new(form_params) respond_to do |format| if @form.save format.html { redirect_to(@form, :notice => 'form was successfully created.') } format.xml { render :xml => @form, :status => :created, :location => @form } else format.html { render :action => "new" } format.xml { render :xml => @form.errors, :status => :unprocessable_entity } end end end def form_params params.require(:form).permit(:name, items_attributes: [:id, :item_name, :_destroy]) end

楷模

# form.rb class Form < ApplicationRecord has_many :items accepts_nested_attributes_for :items, reject_if: :all_blank, allow_destroy: true validates_presence_of :name end # item.rb class Item < ApplicationRecord belongs_to :form end

意见

<!-- _form.html.haml --> = form_for @form do |f| .field = f.label :name %br = f.text_field :name %h3 items #items = f.fields_for :items do |item| = render 'item_fields', f: item .links = link_to_add_association 'add item', f, :items = f.submit .nested-fields .field = f.label :item_name %br = f.text_field :item_name = link_to_remove_association "remove item", f

安慰

tarted POST "/forms" for 127.0.0.1 at 2016-09-08 23:04:17 -0600 Processing by FormsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"FmYPwlXy93iwBMkWvfyd3QJ+NKDcYnraUaKmMISbUIX3+KRL7KtpD33FW3CK+jxvn4AoUUMhx4zrGncJej5BOw==", "form"=>{"name"=>"sfsf", "items_attributes"=>{"1473397456358"=>{"item_name"=>"sfsf", "_destroy"=>"false"}}}, "commit"=>"Create Form"} (0.2ms) BEGIN (0.3ms) ROLLBACK Rendering forms/new.html.haml within layouts/application Rendered forms/_item_fields.html.haml (1.5ms) Rendered forms/_item_fields.html.haml (1.2ms) Rendered forms/_form.html.haml (8.5ms) Rendered forms/new.html.haml within layouts/application (10.1ms) Completed 200 OK in 68ms (Views: 54.5ms | ActiveRecord: 0.5ms)

RAILS 5 PROBLEM. I have a project and I wanted the user to have the option to add extra items to the form. My code renders correctly and it the link to add more fields does add more text field. The problem I have is that is not saving the to my database. I am using mysql2 gem on a MySQl database. I posted the console output. This is what I have:

forms_controller

# forms_controller.rb def new @form = Form.new respond_to do |format| format.html # new.html.erb format.xml { render :xml => @form } end end def create @form = Form.new(form_params) respond_to do |format| if @form.save format.html { redirect_to(@form, :notice => 'form was successfully created.') } format.xml { render :xml => @form, :status => :created, :location => @form } else format.html { render :action => "new" } format.xml { render :xml => @form.errors, :status => :unprocessable_entity } end end end def form_params params.require(:form).permit(:name, items_attributes: [:id, :item_name, :_destroy]) end

models

# form.rb class Form < ApplicationRecord has_many :items accepts_nested_attributes_for :items, reject_if: :all_blank, allow_destroy: true validates_presence_of :name end # item.rb class Item < ApplicationRecord belongs_to :form end

views

<!-- _form.html.haml --> = form_for @form do |f| .field = f.label :name %br = f.text_field :name %h3 items #items = f.fields_for :items do |item| = render 'item_fields', f: item .links = link_to_add_association 'add item', f, :items = f.submit .nested-fields .field = f.label :item_name %br = f.text_field :item_name = link_to_remove_association "remove item", f

console

tarted POST "/forms" for 127.0.0.1 at 2016-09-08 23:04:17 -0600 Processing by FormsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"FmYPwlXy93iwBMkWvfyd3QJ+NKDcYnraUaKmMISbUIX3+KRL7KtpD33FW3CK+jxvn4AoUUMhx4zrGncJej5BOw==", "form"=>{"name"=>"sfsf", "items_attributes"=>{"1473397456358"=>{"item_name"=>"sfsf", "_destroy"=>"false"}}}, "commit"=>"Create Form"} (0.2ms) BEGIN (0.3ms) ROLLBACK Rendering forms/new.html.haml within layouts/application Rendered forms/_item_fields.html.haml (1.5ms) Rendered forms/_item_fields.html.haml (1.2ms) Rendered forms/_form.html.haml (8.5ms) Rendered forms/new.html.haml within layouts/application (10.1ms) Completed 200 OK in 68ms (Views: 54.5ms | ActiveRecord: 0.5ms)

最满意答案

在你的permit / require行(在你的控制器中), item_attribute应该是item_attributes (有多个),即:

def form_params params.require(:form).permit(:name, :date_sent, :quantity, :comment, item_attributes: [:id, :name, :form_id, :_destroy]) end

The reason it wouldn't save is because I was using Rails 5 and I needed to add optional: true to the belongs_to in the item model.

# form.rb class Form < ApplicationRecord has_many :items accepts_nested_attributes_for :items, reject_if: :all_blank, allow_destroy: true validates_presence_of :name end # item.rb class Item < ApplicationRecord belongs_to :form , optional: true end

更多推荐

本文发布于:2023-08-05 05:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428232.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Cocoon   Rails   Ruby   gem

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!