使用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
最满意答案
绑定重定向是.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>
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
绑定重定向是.NET框架概念,.NET Standard和.NET Core上没有绑定重定向。 但是,应用程序(实际的.NET Framework或.NET Core应用程序)需要解析要使用的文件。 在.NET Core上,这是通过基于构建输入生成deps.json文件并且.NET Framework应用程序使用绑定重定向来完成的。 如果需要绑定重定向,则必须将它们添加到使用.NET标准库的.NET Framework应用程序(或库)中。 这些绑定重定向也可以在构建期间根据编译期间使用的程序集自动生成,请参 ...
-
组件Microsoft.Threading.Tasks和Microsoft.Threading.Tasks.Extensions仍然引用System.Runtime和System.Threading.Tasks v1.5.11.0。 没有bindingRedirect , Microsoft.*程序集将尝试加载旧版本的System.*组件,这将失败。 The assemblies Microsoft.Threading.Tasks and Microsoft.Threading.Tasks.Extensio ...
-
我写这个解决了这个问题: AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => { var assembly = Assembly.Load("System.Net.Http.Primitives"); return assembly; }; I solved the problem by writing this: AppDomain.CurrentDomain.AssemblyResolve += ( ...
-
如何确定运行.NET Standard类库的.NET平台?(How to determine the .NET platform that runs the .NET Standard class library?)[2022-05-31]
使用System.Runtime.InteropServices命名空间中的RuntimeInformation.FrameworkDescription属性。 返回一个字符串,指示正在运行应用程序的.NET安装的名称。 该属性返回以下字符串之一: “.NET核心”。 “.NET Framework”。 “.NET Native”。 Use the RuntimeInformation.FrameworkDescription Property from the System.Runtime.Interop ... -
您还需要将.NET标准库添加到测试项目本身。 由于Web API项目等经典项目不支持传递引用,因此还需要将新项目添加到测试项目中。 然后,测试项目的构建逻辑将看到.NET Standard引用并添加兼容性库并执行绑定重定向生成(由于您添加的属性)。 You also need to add the .NET Standard library to the test project itself. Since classic projects like the Web API project don't su ...
-
函数加载到与Azure Functions相同的进程中,因此您只能访问运行时预加载的相同版本的程序集。 这是一个已知问题 ,正在开展工作。 我相信他们正在开发某种“语言服务”,我们可以完全控制加载的程序集。 在此之前,有一些解决方法: Azure功能绑定重定向 Functions are loaded into the same process as Azure Functions, so you only have access to the same version of the assemblies ...
-
SecureString通过System.Security.SecureString NuGet包可用于.NET Core 。 这个NuGet包当前不能在.NET标准库中使用,因为它的实现是特定于.NET Core的。 要使用此软件包,您必须创建一个.NET Core库。 在.NET标准2.0中,这种类型不需要添加NuGet包就可以使用,你可以在.NET标准库中使用它。 SecureString is available for .NET Core via the System.Security.Secur ...
-
这段代码几乎可以告诉整个项目,如果碰巧有任何旧的对MVC 1.0的剩余引用,那么它应该改为尝试使用2.0版本。 我相信这可能会被类库所继承,也可能会忘记更新引用。 What this code does is pretty much tells your entire project that if there happen to be any old leftover references to MVC 1.0, then it should instead go try and use the 2.0 ...