Ruby on Rails:在控制器中,根据HTML ID

时间:2017-01-23 21:49:07

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 controller

在我的RoR应用程序中,我正在尝试创建一个表单,用户可以一次更新多个记录的字段。要做到这一点,我一直在关注这个RailsCast指南http://railscasts.com/episodes/165-edit-multiple?view=asciicast,但这确实告诉我该怎么做。

我遇到的问题是我有一个收件人模型,想要使用不同的数据一次更新多个记录的字段。例如,在此收件人模型中,我有列contact_idinformation,我想要做的是允许用户使用一个表单上的information列的数据更新记录

我的edit_multiple.html.erb表格如下:

<h1>Recipient Specific Information</h1>
<table>
  <tr>
    <th>Contact</th>
    <th>Information</th>
  </tr>
  <%= form_for :recipient, :url => update_multiple_recipients_path, :html => { :method => :put } do |form| %>
    <% for recipient in @recipients %>
        <tr>
            <%= hidden_field_tag "recipient_ids[]", recipient.id %>
            <td><% if not recipient.contact_id.blank? %><%= recipient.contact.firstname %><% elsif not recipient.group_id.blank? %><%= recipient.group.name %><% end %></td>
            <td><%= form.text_field :information, id: recipient.id %></td>
        </tr>
    <% end %>
    <%= form.submit "Submit" %>
  <% end %>
</table>

Recipients_controller

class RecipientsController < ApplicationController
    def edit_multiple
        @recipients = Recipient.where(:email_id => params[:id])
    end
    def update_multiple
        @recipients = Recipient.find(params[:recipient_ids])
        @recipients.each do |recipient|
            recipient.update_attributes(recipient_params)
        end
        flash[:notice] = "Updated products!"
        redirect_to root_path
    end
    private
    def recipient_params
      params.require(:recipient).permit(:contact_id, :information)
    end
end

Routes.rb

resources :recipients do
 collection do
   get :edit_multiple
   put :update_multiple
 end
end

我的问题是,视图上的以下代码显示收件人的联系人姓名和每个收件人的text_field,以便用户可以输入他们想要为每个收件人存储的信息。目前,在更新记录时,会发生的事情是为最后一个记录显示的信息参数输入text_field的数据是为每个记录保存的。应该发生的是,为每条记录输入text_fields的数据应该保存。

我的问题是,是否可以通过控制器中的HTML ID识别视图中的每个文本字段?这是因为我想知道是否可以更改控制器中的以下代码来识别每个文本字段并将输入的数据存储到相应的收件人行中。

def update_multiple
        @recipients = Recipient.find(params[:recipient_ids])
        @recipients.each do |recipient|
            # is it possible to take the data entered in a specific text field?
            recipient.update_attributes(recipient_params)
        end
        flash[:notice] = "Updated products!"
        redirect_to root_path
    end

1 个答案:

答案 0 :(得分:1)

一般情况下,您无法从控制器读取HTML,因为HTML在浏览器上运行,控制器正在服务器上运行。

可以让您的Ruby服务器使用Nokogiri / Mechanize / Selenium等工具读取/解析HTML。但这些工具具有不同的主要用例(网络抓取和自动化测试)。

您可以制作以您希望的格式发送所需数据的表单。您只需要使用表单输入(除非您使用AJAX或websockets或类似的东西)。具体来说,表单输入上的name属性决定了到达服务器的参数的名称。以下是如何创建位于帖子的显示页面上的评论表单的示例。

<form action="/comments" method="POST">
  <input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>">
  <input type="hidden" name="post_id" value="<%= @post.id %>">
  <textarea name="content" placeholder="your comment here"><br>
  <input type="submit" value="add comment">
</form>

当它进入控制器时,它将具有您可以根据您的要求使用的post_idcontent参数。只需确保预防质量分配(使用安全参数)