首页 \ 问答 \ 使用jQuery的Rails中不显眼的动态表单字段(Unobtrusive dynamic form fields in Rails with jQuery)

使用jQuery的Rails中不显眼的动态表单字段(Unobtrusive dynamic form fields in Rails with jQuery)

我试图在Rails中摆脱动态表单域的障碍 - 这似乎是框架不能很好地处理的东西。 我也在我的项目中使用jQuery。 我已经安装了jRails,但是我更希望在可能的情况下不引人注目地编写AJAX代码。

我的形式是相当复杂的,两到三个层次的嵌套并不常见。 我遇到的问题是生成正确的表单ID,因为它们依赖于表单构建器上下文。 我需要能够动态添加新字段或删除has_many关系中的现有记录,我完全失去了。

到目前为止,我看到的每个例子都是以某种方式丑陋的。 Ryan Bates的教程需要RJS,这会在标记中导致一些非常丑陋的突触性JavaScript,并且似乎是在嵌套属性之前编写的。 我看到这个例子的一个叉子没有引人注目的jQuery,但我只是不明白它在做什么,并没有能够使它在我的项目中工作。

有人可以提供一个简单的例子,说明如何做到这一点。 在遵守控制器的REST风格约定的情况下,这甚至是可能的吗?


安迪已经发布了一个删除现有记录的优秀示例,任何人都可以提供创建具有正确属性的新字段的示例? 我无法弄清楚如何用嵌套表单来做到这一点。


I'm attempting to get over the hurdle of dynamic form fields in Rails -- this appears to be something the framework doesn't handle very gracefully. I'm also using jQuery in my project. I have jRails installed, but I'd much rather write the AJAX code unobtrusively where possible.

My forms are fairly complex, two or three levels of nesting are not unusual. The problem I'm having is generating the correct form ids, since they are so dependant on the form builder context. I need to be able to dynamically add new fields or delete existing records in a has_many relationship, and I am completely at a loss.

Every example I've seen so far has been ugly in one way or another. Ryan Bates' tutorial requires RJS, which results in some pretty ugly obtrusive javascript in the markup, and seems to have been written before nested attributes. I've seen a fork of that example with unobtrusive jQuery, but I just don't understand what it's doing, and haven't been able to get it working in my project.

Can somebody provide a simple example of how this is done? Is this even possible while respecting the RESTful convention of the controllers?


Andy has posted an excellent example of deleting an existing record, can anybody provide an example of creating new fields with the correct attributes? I haven't been able to figure out how to do this with nested forms.


原文:https://stackoverflow.com/questions/1704142
更新时间:2022-04-26 20:04

最满意答案

绑定重定向是.NET框架概念,.NET Standard和.NET Core上没有绑定重定向。

但是,应用程序(实际的.NET Framework或.NET Core应用程序)需要解析要使用的文件。 在.NET Core上,这是通过基于构建输入生成deps.json文件并且.NET Framework应用程序使用绑定重定向来完成的。

如果需要绑定重定向,则必须将它们添加到使用.NET标准库的.NET Framework应用程序(或库)中。

这些绑定重定向也可以在构建期间根据编译期间使用的程序集自动生成,请参阅有关自动绑定重定向文档 。 当使用NuGet的新的PackageReference样式使用NuGet包时,这是自动完成的。 由于正确配置会因项目类型而异,请参阅.NET Framework和NuGet的.NET Standard 2.0公告,以获取详细说明。

确保使用正确的绑定重定向的最简单方法是确保.NET Framework应用程序或库设置这些属性(在csproj / vbproj中。对于生成.exe可执行文件但需要单元的项目,不需要第二个属性测试项目):

<PropertyGroup>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>

Binding redirects are a .NET framework concept, there are no binding redirects on .NET Standard and .NET Core.

However, an application (the actual .NET Framework or .NET Core application) need to resolve the files to be used. On .NET Core, this is done by generating a deps.json file based on the build input and a .NET Framework application uses binding redirects.

If a binding redirect is necessary, they have to be added to the .NET Framework application (or library) that used the .NET Standard library.

These binding redirects and also be automatically generated during build, based on the assemblies used during compilation, see the documentation on automatic binding redirects. When using NuGet's new PackageReference style of using NuGet packages, this is done automatically. Since configuring this correctly varies based on the project type, refer to the announcement Issues with .NET Standard 2.0 with .NET Framework & NuGet for detailed descriptions.

The simplest way to make sure that the correct binding redirects are used is to ensure the .NET Framework app or library sets these properties (inside the csproj/vbproj. The second one is not needed for projects that generate .exe executables but needed for unit test projects):

<PropertyGroup>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)