2014-09-09 1 views
0

은 컨트롤러 내에서 인스턴스를 만드는 데 도움이되는 자습서를 따랐습니다. 즉, 엔 z 로프 컨트롤러에서 트랜잭션이 작성됩니다. 블로그 게시물에 대한 댓글을 좋아합니다.중첩 컨트롤러 리소스, 업데이트 및 파괴 방법?

모든 것이 완벽하게 작동하지만 지금 거래를 편집하거나 삭제하는 방법을 모르겠습니다. 내가 필요한 것은 기존의 것을 편집하는 방법을 찾는 것입니다. 제가 지금까지 무엇을 보여 드리죠 :에

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

답변

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

감사합니다 안드레이! 놀랍다, 나는 그것을 시험해보고 싶다 그러나 나는 아직도 form_for와 link_to에서 이러한 행동을 호출하는 방법을 모르겠다. –

+0

나는 다음과 같이 작동한다고 생각한다 : link_to '트랜잭션 파괴', envelope_transaction_path (@ envelope, transaction) delete ' –

+0

대부분 작동합니다. 트랜잭션 컨트롤러에서 해당 액션을 호출하면 nil : nilclass에 대해 정의되지 않은 메소드 'destroy'가 발생합니다. 그래서 나는 트랜잭션 객체 나 인스턴스가 거기에 있지 않을 수도 있다고 생각한다. 당신의 도움에 감사 드리며 몇 가지 시도해 보겠습니다. –