按照教程帮助我在控制器中创建实例。换句话说,在信封控制器上创建交易。喜欢博客文章中的评论。
一切都很完美,但我现在不知道如何编辑交易或销毁交易。我只需要找到如何编辑现有的东西。让我告诉你到目前为止我所拥有的:
在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
答案 0 :(得分:1)
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