0

このエラーメッセージが表示されます

金額は空白にできません

作成した金額に基づいて支払いを作成したいのですがbefore_filter {@amount=Amount.new}、作成した金額を支払い方法に割り当てません。そのため、新しい金額を作成した後でも、支払いで上記のエラーが発生し続けます。

支払いコントローラー

before_filter {@amount =Amount.new}

def new
 @payment = Payment.new
 respond_to do |format|
  format.html 
  format.json { render :json => @order }
end
end

def create
  @payment = @amount.payments.build(params[:payment])      
  if @payment.save       
     if @payment.purchase
       render :action=> "success"
      else
        render :action=> "failure"
     end
  else
    render :action => "new" 
  end       
end  

アマウントコントローラー

def create
@amount = current_user.amounts.build(params[:amount])
respond_to do |format|
  if @amount.save
    format.html { redirect_to root_url, :notice => 'Amount was successfully created.' }
    format.json { render :json => @amount, :status => :created, :location => @amount }
  else
    format.html { render :action => "new" }
    format.json { render :json => @amount.errors, :status => :unprocessable_entity }
  end
end
end


def new
 @amount = Amount.new
 respond_to do |format|
  format.html 
  format.json { render :json => @amount }
 end
end

支払いモデル

belongs_to :amount

金額モデル

 has_many :payments
4

1 に答える 1

0

ガイドマニュアルによると、「フィルターはコントローラーのスコープ内で実行されない」ので@amount、実際には、コントローラーActiveSupport::Callbacks::Callbackではなく一部に変数を設定しています。

lambda小さな塊の前に置くことでこれを修正できるかもしれないと思います。そうでない場合は、メソッドを定義し、:set_amountそこにコードを配置する必要があります。

参照:

railsはbefore_filterをどのように実装しますか?

http://guides.rubyonrails.org/action_controller_overview.html

お役に立てば幸いです。

于 2013-01-24T15:08:02.950 に答える