NNNNNNNNNNDelicious
NNNNNNNNNNDelicious

Reputation: 983

Rails Destroy Record link doesn't do anything

I'm attempting to destroy a nested record, the destroy button appears to work and redirect back to the above models page but the record still shows up there.

Here is my partial for the nested record:

<p>
    <tr>
        <td><%= time_delta.start %></td>
        <td><%= time_delta.length %></td>
        <td>
            <%=
                link_to 'Destroy Time Delta',
                [time_delta.stock, time_delta],
                method: :delete,
                data: { confirm: 'Are you sure?' }
            %>
        </td>
    </tr>
</p>

Heres the show for the above class

<!-- Shows the stock-->
<h1> Stock </h1>
<table> 
    <tr>
        <th>Stock</th>
        <th>Hashtag</th>        
    </tr>
        <tr>
            <td><%= @stock.name %></td>
            <td><%= @stock.hashtag %></td>
        </tr>
</table>

<!-- Shows the Time Detlas-->
<h3>TimeDeltas: </h2>
<%= render @stock.time_deltas %>

<!-- Creates a Time Delta-->
<h3>Add a TimeDelta:</h2>
<%= render "time_deltas/form" %>

<%= link_to 'Back', stocks_path%>
<%= link_to 'Edit', edit_stock_path(@stock)%>

Here is the controller for the Time Delta (The nested class)

class TimeDeltasController < ApplicationController

  def create
    @stock = Stock.find(params[:stock_id])
    @time_delta = @stock.time_deltas.create(time_delta_params)
    redirect_to stock_path(@stock)
  end

  def destroy
    @stock = Stock.find(params[:stock_id]) 
    @time_delta = @stock.time_deltas.create(params[:time_delta_id])

    # @time_delta = @stock.time_deltas.create(time_delta_params)
    @time_delta.destroy
    redirect_to stock_path(@stock)
  end

  private 
    def time_delta_params
      params.require(:time_delta).permit(:start, :length)
    end
end

Upvotes: 0

Views: 74

Answers (1)

Doguita
Doguita

Reputation: 15703

You are creating a new time_delta on the action and calling destroy on it. What you need is find the time_delta which you want to destroy.
Change the second line in destroy action to:
@time_delta = @stock.time_deltas.find(params[:id])

Upvotes: 1

Related Questions