English 中文(简体)
铁路3 提交有多个记录的形式
原标题:Rails 3 submit a form with multiple records

我给铁路带来了新的东西,因此,这或许是一个基本问题。 我试图创造一种形式,使用户能够一劳永逸地制作3个记录。 我只想让用户点击提交纽顿。 我向我的审查模式提交名称、评论和评级。 目前,只有最后的记录输入数据库。

<%= form_for([@user,@review]) do |f| %>
<table>
  <tr>
    <td>Rank</td>
    <td>Name</td>
    <td>Comment</td>
  </tr>
  <tr>
    <td>1</td>
    <td><%= f.text_field :name %></td>
    <td><%= f.text_field :comment %></td>
    <%= f.hidden_field :rating, :value=> "5" %>
  </tr>
  <tr>
    <td>2</td>
    <td><%= f.text_field :name %></td>
    <td><%= f.text_field :comment %></td>
    <%= f.hidden_field :rating, :value=> "3" %> 
  </tr>
  <tr>
    <td>3</td>
    <td><%= f.text_field :name %></td>
    <td><%= f.text_field :comment %></td>
    <%= f.hidden_field :rating, :value=> "1" %>
  </tr>
</table>
  <div class="actions">
    <%= f.submit "Create my top 3" %>
  </div>
<% end %>

欢迎任何建议。 感谢。

最佳回答

我建议为此使用<代码>领域_:

<%= form_for([@user, :reviews]) do |f| %>
  <% @reviews.each do |review| %>
    <%= fields_for review do |r| %>
      <%= render "reviews/form", :r => r %>
    <% end %>
  <% end %>
<% end %>

为了完成这一工作,您将需要按照您的控制权要求,建立尽可能多的审查对象:

def new
  # you could also have this in a before_filter...
  @user = User.find(params[:id])
  @reviews = Array.new(3) { @user.reviews.build }
end

这将为这一用户创建新的<>审查记录<>,不同于新的<>记录<>。 事例只是Ruby。 现在是因为您称之为@user.reviews.build。 阁下认为,三次审查。

def create
  @user = User.find(params[:id])

  @reviews = Review.create(params[:reviews])
  # Some more logic for validating the parameters passed in
end

这将产生三个新的<条码>审查物体,并将其与<条码>@user连接,假定这三个物体均为有效。

问题回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签