Rails Destroy Record链接没有做任何事情

时间:2014-04-23 20:53:19

标签: ruby-on-rails

我正在尝试销毁嵌套记录,销毁按钮似乎可以工作并重定向回上面的模型页面,但记录仍显示在那里。

以下是嵌套记录的部分内容:

<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>

继承上述课程的节目

<!-- 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)%>

以下是时间增量(嵌套类)的控制器

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

1 个答案:

答案 0 :(得分:1)

您正在操作上创建一个新的time_delta并在其上调用destroy。你需要的是找到你想要销毁的time_delta 将destroy动作中的第二行更改为:
 @time_delta = @stock.time_deltas.find(params[:id])