I have an INCIDENT with an attached WITNESS.

I am trying to show a link to remove an attachment from a nested attribute, but my link is pulling the :id of the parent record (invoice.id) instead of the nested/child record (invoice.witness_id).

I know I'm doing something wrong in my routes or in calling the correct id number from the controller or view... any help is appreciated!

incident.rb

  has_many :witnesses
  accepts_nested_attributes_for :witnesses, :reject_if => :all_blank, :allow_destroy => true

witness.rb

  belongs_to :incident  
  has_attached_file :statement

routes.rb

match 'witness/:id' => 'witnesses#remove_statement', via: [:get, :post], as: 'remove_statement'

witnesses_controller

  def index
    @witnesses = @incident.witnesses.all
  end

  def remove_statement
    @witness = Witness.find(params[:id])
    @witness.statement = nil
    respond_to do |format|
        if @witness.save
          format.html { redirect_to :back, notice: 'Attachment was removed.' }
          format.json { head :no_content }
        else
          format.html { redirect_to :back, error: 'Attachment could not be removed.'  }
          format.json { render json: @witness.errors, status: :unprocessable_entity }
        end
    end
     end

  private
    def set_witness
      @witness = @incident.witnesses.find(params[:id])
    end

    def witness_params
      params[:witness].permit(:first_name, :last_name, :phone, :email, :statement, :incident_id)
    end

_witness_fields partial

<div class="nested-fields">
  <div class="form-group">
    ....
    <%= link_to "Remove Attachment", remove_statement_path, :id => :witness_id  %>
    ...

incidents/_form.html.erb

<%= form_for(@incident, html: { :multipart => true , class: 'form-horizontal' }) do |f| %>
  <%= f.error_notification %>

    <% if @incident.errors.any? %>
    <div class="red">
      <% @incident.errors.full_messages.each do |msg| %>
       <%= msg %><hr>
      <% end %>
    </div>
  <% end %>
.....

<!-- WITNESS SECTION -->
<div class="span6">
<hr>
    <fieldset id="witnesses">
        <%= f.fields_for :witnesses do |builder| %>
          <%= render 'witness_fields', :f => builder %>
        <% end %>    
    </fieldset>
    <p class="links">
      <%= link_to_add_association 'Add Witness/Contact', f, :witnesses, { class:"btn btn-primary" } %>
        </p>
    </div>
    </div>
    <!-- END WITNESSES SECTION -->
.....
有帮助吗?

解决方案

In your _withness_fields partial, you write

<%= link_to "Remove Attachment", remove_statement_path, :id => :witness_id  %>

That should be something like

<%= link_to "Remove Attachment", remove_statement_path(f.object.id)  %>

So two things: the path helper remove_statement_path needs the id as a parameter, and secondly, you need to actually give it the correct id of the object for which you are currently rendering.

Please note, since you dynamically add these, for new records this will not be valid (since there is no idea).

So you will have to check if the record is a new_record? and only show that link if it is not (because then you will have a valid id). If it is not a new record, you can just use the cocoon helper to remove it.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top