如何防止 Rails 以与新字段不同的方式处理编辑字段_For
我通过 couchrest 使用 Rails3 beta3 和 couchdb。我没有使用活动记录。
我想将多个“部分”添加到“指南”中,并通过一些 JavaScript 动态添加和删除部分。我看过 Ryan Bates 的所有截屏视频,他们给了我很大帮助。唯一的区别是我想将所有部分保存为部分数组而不是单个部分。基本上是这样的:
"sections" => [{"title" => "Foo1", "content" => "Bar1"}, {"title" => "Foo2", "content" => "Bar2"}]
所以,基本上我需要参数哈希在提交表单时看起来像这样。当我创建表单时,我正在执行以下操作:
<%= form_for @guide, :url => { :action => "create" } do |f| %>
<%= render :partial => 'section', :collection => @guide.sections %>
<%= f.submit "Save" %>
<% end %>
我的部分部分如下所示:
<%= fields_for "sections[]", section do |guide_section_form| %>
<%= guide_section_form.text_field :section_title %>
<%= guide_section_form.text_area :content, :rows => 3 %>
<% end %>
好的,所以当我创建带有部分的指南时,它按照我的意愿完美地工作。 params 哈希给了我一个像我想要的那样的节数组。当我想要编辑指南/部分并再次保存它们时,问题就出现了,因为 Rails 正在将指南的 id 插入每个表单字段的 id 和名称中,这会破坏表单提交时的参数哈希。
需要明确的是,这里是新资源的原始表单输出:
<input type="text" size="30" name="sections[][section_title]" id="sections__section_title">
<textarea rows="3" name="sections[][content]" id="sections__content" cols="40"></textarea>
这是编辑现有资源时的样子:
<input type="text" value="Foo1" size="30" name="sections[cd2f2759895b5ae6cb7946def0b321f1][section_title]" id="sections_cd2f2759895b5ae6cb7946def0b321f1_section_title">
<textarea rows="3" name="sections[cd2f2759895b5ae6cb7946def0b321f1][content]" id="sections_cd2f2759895b5ae6cb7946def0b321f1_content" cols="40">Bar1</textarea>
How do I Force Rails to always use the new resource behavior and notautomatically add the id to the name和价值。我必须创建自定义表单生成器吗?我还可以采取其他一些技巧来防止 Rails 将指南的 id 放在那里吗?我尝试了很多东西但没有任何作用。
提前致谢!
I am using rails3 beta3 and couchdb via couchrest. I am not using active record.
I want to add multiple "Sections" to a "Guide" and add and remove sections dynamically via a little javascript. I have looked at all the screencasts by Ryan Bates and they have helped immensely. The only difference is that I want to save all the sections as an array of sections instead of individual sections. Basically like this:
"sections" => [{"title" => "Foo1", "content" => "Bar1"}, {"title" => "Foo2", "content" => "Bar2"}]
So, basically I need the params hash to look like that when the form is submitted. When I create my form I am doing the following:
<%= form_for @guide, :url => { :action => "create" } do |f| %>
<%= render :partial => 'section', :collection => @guide.sections %>
<%= f.submit "Save" %>
<% end %>
And my section partial looks like this:
<%= fields_for "sections[]", section do |guide_section_form| %>
<%= guide_section_form.text_field :section_title %>
<%= guide_section_form.text_area :content, :rows => 3 %>
<% end %>
Ok, so when I create the guide with sections, it is working perfectly as I would like. The params hash is giving me a sections array just like I would want. The problem comes when I want edit guide/sections and save them again because rails is inserting the id of the guide in the id and name of each form field, which is screwing up the params hash on form submission.
Just to be clear, here is the raw form output for a new resource:
<input type="text" size="30" name="sections[][section_title]" id="sections__section_title">
<textarea rows="3" name="sections[][content]" id="sections__content" cols="40"></textarea>
And here is what it looks like when editing an existing resource:
<input type="text" value="Foo1" size="30" name="sections[cd2f2759895b5ae6cb7946def0b321f1][section_title]" id="sections_cd2f2759895b5ae6cb7946def0b321f1_section_title">
<textarea rows="3" name="sections[cd2f2759895b5ae6cb7946def0b321f1][content]" id="sections_cd2f2759895b5ae6cb7946def0b321f1_content" cols="40">Bar1</textarea>
How do I force rails to always use the new resource behavior and not automatically add the id to the name and value. Do I have to create a custom form builder? Is there some other trick I can do to prevent rails from putting the id of the guide in there? I have tried a bunch of stuff and nothing is working.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我想我已经找到了一些可行的方法。将部分的第一行更改为:
似乎工作得很好。这样,新表单和编辑表单在底层看起来都是一样的,并且 params 哈希的工作方式就像我需要的那样。如果有人发现此方法有任何问题或有其他选择,请告诉我。
Ok, I think I have figured out something that works. Changing the first line of the partial to:
Seems to work just fine. This way both new and edit form looks the same under the hood and the params hash works just like I need it to. If anyone sees anything wrong with this or has another alternative, please let me know.