简体   繁体   English

Rails 7 中的茧宝石问题

[英]Cocoon gem issue in Rails 7

I want to implement nested form using cocoon gem.我想使用 cocoon gem 实现嵌套形式。

There is no error but when I click on the item link to edit and trigger it, nothing works.没有错误,但是当我单击项目链接进行编辑和触发时,没有任何效果。 However, the URL chnages from http://127.0.0.1:3000/portfolios/2/edit to http://127.0.0.1:3000/portfolios/2/edit# .但是,URL 从http://127.0.0.1:3000/portfolios/2/edit 更改http://127.0.0.1:3000/portfolios/2/edit#

I expect the input tag to show, but it does not.我希望输入标签显示,但它没有。

View看法

_form.html.erb _form.html.erb

 <div class="form-group mb-3"> <h2>Technologies used: </h2> <div> <%= form.fields_for:technologies do |technology_form|%> <%= render 'technology_fields', f: technology_form %> <% end %> <div> <%= link_to_add_association 'Add Technology', form, :technologies %> </div> </div> </div>

_technolog_fields.html.erb _technolog_fields.html.erb

 <div class="form-group mb-1 nested-fields"> <%= f.label:name %> <%= f.text_field:name, class: 'form-control' %> </div>

Model Model

portfolio model投资组合 model

 class Portfolio < ApplicationRecord has_many:technologies accepts_nested_attributes_for:technologies, reject_if: ->(attrs) { attrs['name'].blank? } end

I add cocoon gem to Gemfile and run bundle install我将 cocoon gem 添加到 Gemfile 并运行 bundle install

Alse, I add //= require cocoon to application.js另外,我将 //= require cocoon 添加到 application.js

When I click on add technology, nothing happens.当我点击添加技术时,没有任何反应。

I will appreciate your help.我会感谢你的帮助。

Thanks.谢谢。

Are you using importmap with Rails 7?你在 Rails 7 中使用 importmap 吗? If so, then you'll need to change the way you include the JS.如果是这样,那么您需要更改包含 JS 的方式。

First in your terminal you would:首先在你的终端你会:

./bin/importmap pin @nathanvda/cocoon

Then, somewhere in your JS you would:然后,在你的 JS 中的某个地方,你会:

import "@nathanvda/cocoon"

PS - you'll also need to pull in jQuery, either through a gem or perhaps again using importmaps. PS - 您还需要通过 gem 或再次使用 importmaps 获取 jQuery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM