Ruby on Rails:同一表单中的多个输入字段 - 更改ID /值

时间:2022-12-09 15:15:17

Have a page where there are multiple input fields of the same thing, Posts. Right now, when a user enters in a question for, let's say 3 fields, the only one that saves to the database is the last one. Whereas, it should save all three and give them each it's own post_id. Also; if the user doesn't enter anything in for the other fields, it should not save in the database either.

有一个页面,其中有相同的东西的多个输入字段,帖子。现在,当用户输入问题时,假设3个字段,保存到数据库的唯一字段是最后一个。然而,它应该保存所有三个并给它们每个自己的post_id。也;如果用户没有为其他字段输入任何内容,则它也不应保存在数据库中。

new_step_4_html.erb

<%= form_for(@post) do |f| %>
  <%= f.text_field :content %>
  <%= f.text_field :content %>
  <%= f.text_field :content %>
<% end %>

projects_controller.rb

def new_step_4
  @post = Post.new
end

Right now, all it does is submit one :content field, obviously because they all share the same id/value. Unfortunately, the Railscasts #197 applies for nested forms, so the javascript and helper stuff he does all applies for nested. I would think this is something simple. Person from IRC mentioned I could do some sort of '3.times' code into the view file or something?

现在,它只是提交一个:content字段,显然是因为它们都共享相同的id /值。不幸的是,Railscasts#197适用于嵌套表单,因此他所做的javascript和帮助程序都适用于嵌套。我认为这很简单。来自IRC的人提到我可以在视图文件中做某种“3次”代码或其他什么?

1 个解决方案

#1


3  

First of all you will probably have to edit the model of you post.

首先,您可能需要编辑您发布的模型。

post.rb

has_many :contents, :dependent => :destroy
accepts_nested_attributes_for :contents

You will need another model to store the content fields. so first generate a model

您将需要另一个模型来存储内容字段。所以首先生成一个模型

rails g model content post_id:integer body:text

the model

content.rb

belongs_to  :post

Now, in stead of doing <%= f.text_field :content %> a few times, let rails create them, because now you basically let them overwrite each other.

现在,让我们多次执行<%= f.text_field:content%>,让rails创建它们,因为现在你基本上让它们相互覆盖。

3.times do
  content = @post.content.build
end

the form view will be something like this:

表单视图将是这样的:

<%= form_for @post do |f| %>
  <%= f.fields_for :contents do |builder| %>
    <%= builder.label :body, "Question" %><br />
    <%= builder.text_area :body, :rows => 3 %><br /> 
  <%= end %>     
  <p><%= f.submit "Submit" %></p>
<% end %>

I did not test this code, but the idea should be correct. Let me know if you need more info.

我没有测试这段代码,但这个想法应该是正确的。如果您需要更多信息,请告诉我。

#1


3  

First of all you will probably have to edit the model of you post.

首先,您可能需要编辑您发布的模型。

post.rb

has_many :contents, :dependent => :destroy
accepts_nested_attributes_for :contents

You will need another model to store the content fields. so first generate a model

您将需要另一个模型来存储内容字段。所以首先生成一个模型

rails g model content post_id:integer body:text

the model

content.rb

belongs_to  :post

Now, in stead of doing <%= f.text_field :content %> a few times, let rails create them, because now you basically let them overwrite each other.

现在,让我们多次执行<%= f.text_field:content%>,让rails创建它们,因为现在你基本上让它们相互覆盖。

3.times do
  content = @post.content.build
end

the form view will be something like this:

表单视图将是这样的:

<%= form_for @post do |f| %>
  <%= f.fields_for :contents do |builder| %>
    <%= builder.label :body, "Question" %><br />
    <%= builder.text_area :body, :rows => 3 %><br /> 
  <%= end %>     
  <p><%= f.submit "Submit" %></p>
<% end %>

I did not test this code, but the idea should be correct. Let me know if you need more info.

我没有测试这段代码,但这个想法应该是正确的。如果您需要更多信息,请告诉我。