ruby on rails - Nested controller Resources, how to do update and destroy? -
followed tutorial me create instances within controller. in other words transactions created on envelope controller. comments on blog post.
everything working perfectly, don't know how edit transaction or destroy one. need find how edit existing thing. let me show have far:
in views/envelopes/edit (the form code copied can create new transactions)
<% @envelope.transactions.each |transaction|%> <%= form_for [@envelope, @envelope.transactions.build] |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 remove transaction ???--> <% end %> in routes.rb
resources :envelopes resources :transactions end in transaction controller
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 @envelope, notice: 'transaction updated' else redirect_to @envelope, notice: 'transaction not updated' end end def destroy @transaction.destroy redirect_to @envelope, notice: 'text here' end
Comments
Post a Comment