嵌套控制器资源,如何进行更新和销毁



遵循教程帮助我在控制器中创建实例。换句话说,事务是在信封控制器上创建的。喜欢博客文章上的评论。

一切都很顺利,但我现在不知道如何编辑交易或销毁交易。我所需要的只是找到如何编辑现有的东西。让我给你看看我目前拥有的:

在视图/信封/编辑中(表单代码是从您可以创建新交易的地方复制的)

 <% @envelope.transactions.each do |transaction|%>
   <%= form_for [@envelope, @envelope.transactions.build] do |f| %> <!--??? NEED EDIT INSTEAD OF BUILD ???-->
     <%= f.text_field :name, "value" => transaction.name %>
     <%= f.text_field :cash, "value" => transaction.cash %>
     <%= f.submit "Submit" %> 
   <% end %>
   <%= link_to "Remove", root_path %>  <!--??? WANT TO REMOVE TRANSACTION ???-->
 <% end %>

在路线上。rb

  resources :envelopes do 
    resources :transactions
  end

事务控制器

class TransactionsController < ApplicationController
  def create
    @envelope = Envelope.find(params[:envelope_id])
    @transaction = @envelope.transactions.build(transaction_params)#(params[:transaction])
    @transaction.save
    @envelope.update_attributes :cash => @envelope.cash - @transaction.cash
    redirect_to edit_envelope_path(@envelope)
  end
  def destroy
    # ???
  end
  def update
    # ???
  end
  def transaction_params
    params.require(:transaction).permit(:cash, :name, :envelope_id)
  end
end
  def update
    @transaction = @envelope.transactions.find(params[:id])
    if @transaction.update(transaction_params)
      redirect to @envelope, notice: 'Transaction was successfully updated'
    else
      redirect_to @envelope, notice: 'Transaction was not updated'
    end
  end
  def destroy
    @transaction.destroy
    redirect_to @envelope, notice: 'Text here'
  end

最新更新