【发布时间】:2014-09-09 19:35:04
【问题描述】:
按照教程帮助我在控制器中创建实例。换句话说,事务是在信封控制器上创建的。就像博客文章中的 cmets。
一切正常,但我现在不知道如何编辑交易或销毁交易。我所需要的只是找到如何编辑现有的东西。让我向您展示我目前所拥有的:
在views/envelopes/edit中(表单代码是从您可以创建新交易的地方复制的)
<% @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 %>
在 routes.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
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 nested-resources