动态添加嵌套表单

发布于 2024-10-10 15:37:30 字数 1541 浏览 0 评论 0原文

我一直在关注这个railscast http://media.railscasts.com/videos/074_complex_forms_part_2.mov< /a>

我有一个任务和步骤。每个任务可以有多个步骤。

我试图通过单击链接来添加嵌套表单。 Railscast 显示的内容与我所拥有的内容之间的区别在于,我在步骤控制器中获得了步骤表单,但这应该不是问题。

我也在使用 Rails3 和 jQuery,但还没有看到任何关于如何在 Rails3 中执行此操作的教程。

我的任务/new.html.erb

<%= form_for @task, :html=>{:multipart => true do |f| %>
  <%= render 'form', :f=>f %>
< end >

用于我的任务/_form.html.erb

  <%= f.label :task_name %>
  <%= f.text_field :task_name %>

 <%= f.label :media %>
 <%= f.file_field :media %>
      < div id="steps" >
          <%= render 'steps/form', :f=> f % >
      < /div>
     <%= link_to_function "Add Step" do |page|
         page.insert_html :bottom, :steps, :partial=>'steps/form', :object => Step.new end %>
<%= f.submit %>

步骤/form.html.erb

<p class="fields">
    <%= fields_for :steps do |builder| %>
       <%= builder.label :title >
       <%= builder.text_field :title >
       <%= builder.label :description %>
       <%= builder.text_area :description %>
    <% end %>
</p>

页面加载正常,但是当我单击“添加步骤链接”时,出现两个 JavaScript 错误。

RJS error:
 TypeError: Element.insert is not a function

然后

Element.insert("steps", {bottom: "<p class=\"fields\">\n\t</p>\t"});

I've been following this railscast http://media.railscasts.com/videos/074_complex_forms_part_2.mov

I've got a task and steps. Each task can have many steps.

I'm attempting to add a nested form by clicking a link.
the difference between what the railscast shows, and what I have is that i've got my steps form in my steps controller, but that shouldn't be a problem.

I'm also using rails3 with jQuery, but haven't seen any tutorials on how to do this in rails3.

My task/new.html.erb

<%= form_for @task, :html=>{:multipart => true do |f| %>
  <%= render 'form', :f=>f %>
< end >

for my task/_form.html.erb

  <%= f.label :task_name %>
  <%= f.text_field :task_name %>

 <%= f.label :media %>
 <%= f.file_field :media %>
      < div id="steps" >
          <%= render 'steps/form', :f=> f % >
      < /div>
     <%= link_to_function "Add Step" do |page|
         page.insert_html :bottom, :steps, :partial=>'steps/form', :object => Step.new end %>
<%= f.submit %>

steps/form.html.erb

<p class="fields">
    <%= fields_for :steps do |builder| %>
       <%= builder.label :title >
       <%= builder.text_field :title >
       <%= builder.label :description %>
       <%= builder.text_area :description %>
    <% end %>
</p>

The page loads fine, but when I click the 'add step link', I get two javascript errors.

RJS error:
 TypeError: Element.insert is not a function

then

Element.insert("steps", {bottom: "<p class=\"fields\">\n\t</p>\t"});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素年丶 2024-10-17 15:37:30

link_to_function 在rails3 中不再有效。

我所做的是
1) 观看此截屏http://railscasts.com/episodes/205-unobtrusive-javascript

而不是 'link_to_function,我现在有

<% link_to "Add Step", @step, :remote=>true, :class=>'addStep'%>

然后我有一个 content_for 来处理和显示 javascript (jQuery)

$('a.addStep').click(function(){
   $('div#newStep').html("<= escape_javascript(render('steps/form'))%>");
})

这是将表单添加到页面,但它并没有全部连接起来,因为渲染没有连接完成任务的步骤。还不完全确定如何编写,但表单现在正在添加到页面中,只是无法正确提交。

The link_to_function is no longer valid in rails3.

What I did was
1) watch this screencast http://railscasts.com/episodes/205-unobtrusive-javascript

instead of the 'link_to_function, I now have

<% link_to "Add Step", @step, :remote=>true, :class=>'addStep'%>

Then I've got a content_for to process and display the javascript (jQuery)

$('a.addStep').click(function(){
   $('div#newStep').html("<= escape_javascript(render('steps/form'))%>");
})

This is adding the form to the page, but it isn't all wired up as the render isn't connecting the step to the task. Not entirely sure how to write that yet, but the form is now being added to the page, it just won't submit properly .

单身狗的梦 2024-10-17 15:37:30

你应该尝试一下这个 gem https://github.com/ryanb/nested_form 这样更容易设置起来没有任何麻烦。您是否还使用 http://railscasts 查看了 Rail-casts。 com/episodes/196-nested-model-form-part-1http://railscasts.com/episodes/197-nested-model-form-part-2。这些是复杂形式的全新形式,而不是嵌套模型,同样的事情。

去掉所有的“h”,并确保将应用程序帮助程序底部的 Jquery 行更改为:

link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")

此线程可以指导您:rails fields_for render parts with multiple locals generated undefined-variable

另请阅读 Rails 强制转换的评论区域,因为它们通常具有更新的代码一直到今天。

You should try out this gem https://github.com/ryanb/nested_form so its way easier to set it up without any headaches. Have you also checked out the Rail-casts with http://railscasts.com/episodes/196-nested-model-form-part-1 and http://railscasts.com/episodes/197-nested-model-form-part-2. Those are the brand new ones of the Complex Forms, instead its Nested Models, same thing.

Get rid of all of the 'h's and be sure to change the Jquery line at the bottom of the application helper to:

link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")

This thread can guide you: rails fields_for render partial with multiple locals producing undefined variable

Also read the comments area of the Rails casts because they usually have updated code all the way to today.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文