lundi 23 janvier 2017

Ruby on Rails: In controller, retrieve data from text_field based on HTML ID

In my RoR application, I am trying to create a form whereby users can update the fields of multiple records at once. To do this I have been following this RailsCast guide http://ift.tt/29dnsZk but this does fully show me what to do.

The problem I have is that I have a Recipient model and want to update the fields of multiple records at once with different data. For example, in this Recipient model I have the columns contact_id and information and what I want to do is allow a user to update the records with the data for the information column on one form.

My edit_multiple.html.erb form is as follows:

<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

My problem is that the following code on the view displays the recipient's contact name and a text_field for each one recipient so that the user can enter the information they want to store for each recipient. Currently, when it comes to updating the records, what happens is the data entered into the text_field for the information parameter on the record display last is saved for each of the records. What should happen is that the data entered into of text_fields for each of the records should get saved.

My question is whether it is possible to identify each of the text fields on the view via their HTML ids in the controller? This is because I am wondering whether it would then be possible to change the below code in the controller to identify each text field and store the data entered into the corresponding recipient row.

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

Aucun commentaire:

Enregistrer un commentaire