我已经看过几次这个问题,并尝试了推荐的方法来解决该问题。我的form_for仍然遇到此错误:
First argument in form cannot contain nil or be empty
我有几个字段应输入到“收费”对象中。我希望表单提交并创建一个收费对象,其中包括表单中包含的正确字段,包括隐藏字段(user_id和gift_id)。
楷模:
class Gift
has_many :codes
has_many :charges
class Code
belongs_to :gift
class Charge
belongs_to :user
belongs_to :gift
class User
has_many :charges
charges_controller.rb
class ChargesController < ApplicationController
skip_before_filter :verify_authenticity_token, :only => [:new, :create]
def index
@charges = Charge.all
end
def show
@charge = Charge.find(params[:id])
end
def new
@charge = Charge.new
end
def create
@charge = Charge.new(params[:charge_params])
Stripe.api_key = "sk_test_Ig7H6rhd0jl7W3V48y5pleOa"
token = params[:stripeToken]
customer = Stripe::Customer.create(
:card => token,
:description => "[email protected]"
)
Stripe::Charge.create(
:amount => gift.price,
:currency => "cad",
:customer => customer.id
)
save_stripe_customer_id(user, customer.id)
# Later...
customer_id = get_stripe_customer_id(user)
Stripe::Charge.create(
:amount => 1500,
:currency => "cad",
:customer => customer_id
)
if @charge.save
redirects_to gifts_path, flash[:notice] = 'It worked'
else
render 'gifts/new'
end
end
def edit
@charge = Charge.find(params[:id])
end
def update
@charge = Charge.find(params[:id])
@charge.update_attributes(charge_params)
end
def destroy
@charge = Charge.find(params[:id])
@charge.destroy
end
private
def charge_params
params.require(charge).permit(:sender_name, :recipient_email, :recipient_msg, :stripeToken, :gift_id, :user_id)
end
end
_new.html.erb(收集数据的表单)
<%= form_for @charge do |f| %>
<%= f.hidden_field :gift_id %>
<%= f.hidden_field :user_id %>
<div class="form-group left-inner-addon">
<i class="glyphicon glyphicon-send"></i>
<%= f.text_field :sender_name, :class => "form-control", :placeholder => "Sender's Name" %>
</div>
<div class="form-group left-inner-addon">
<i class="glyphicon glyphicon-send"></i>
<%= f.email_field :recipient_email, :class => "form-control", :placeholder => "Recipient's Email" %>
</div>
<div class="form-group left-inner-addon">
<i class="glyphicon glyphicon-pencil"></i>
<%= f.text_field :recipient_msg, :class => "form-control", :placeholder => "Message to Recipient" %>
</div>
<div class="form-group left-inner-addon ">
<i class="glyphicon glyphicon-credit-card"></i>
<%= f.text_field "data-stripe" => "number", :class= => "form-control", :placeholder => "Credit Card Number" %>
</div>
<div class="form-group">
<div class="row">
<div class="col-xs-4 left-inner-addon ">
<i class="glyphicon glyphicon-calendar"></i>
<%= f.text_field "data-stripe" => "exp-month", :class => "form-control", :placeholder => "MM" %>
</div>
<div class="col-xs-5 left-inner-addon ">
<i class="glyphicon glyphicon-calendar"></i>
<%= f.text_field "data-stripe" => "exp-year", :class => "form-control", :placeholder => "YYYY" %>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-xs-5 left-inner-addon ">
<i class="glyphicon glyphicon-ok-circle"></i>
<%= f.text_field "data-stripe" => "cvc", :class => "form-control", :placeholder => "CVC" %>
</div>
</div>
</div>
<%= f.submit :class => "btn btn-primary", :type => "submit" %>
<% end %>
_show.html.erb(呈现表单的部分)
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title"><center><%= gift.name %></center></h4>
</div>
<div class="modal-body">
<p><%= image_tag gift.picture.url(:mobile) %></p>
<p><%= gift.description %></p>
<p>$<%= gift.price %></p>
<%= render partial: 'charges/new', :locals => { :gift => gift } %>
</div>
<div class="modal-footer">
</div>
index.html.erb(呈现_show.html.erb部分的页面)
<%= title "Redwood | Gifts" %>
<div class="container">
<div class="row" id="item">
<% @gifts.each do |gift| %>
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<%= image_tag gift.picture.url(:medium) %>
<div class="caption">
<h3><%= gift.name %></h3>
<h4><%= link_to gift.merchant.name, merchant_path(gift.merchant.id) %></h4>
<p><%= gift.description %></p>
<p><%= link_to 'Buy', gift_path(gift), :remote => true, 'data-toggle' => "modal", 'data-target' => "#myModal#{gift.id}", :class => "btn btn-primary", 'data-type' => "html" %></p>
</div>
<!-- Modal -->
<div class="modal fade" id="myModal<%= gift.id %>">
<div class="modal-dialog">
<div class="modal-content">
<%= render partial: 'show', :locals => { :gift => gift } %>
</div>
</div>
</div>
<!-- End of Modal -->
</div>
</div>
<% end %>
</div>
</div>
最佳答案
您的“新”部分由“显示”部分呈现,然后该“显示”部分由“索引”视图呈现。这意味着运行的只是“索引”操作中的代码。由于您没有在索引操作中实例化名为@charge的实例变量,因此@charge将在'new'部分中返回nil。这就是为什么您会收到该错误。
要解决该错误,您应该将以下代码添加到索引操作中:
@charge = Charge.new
这样,@ charge将返回Charge的实例,而不是nil。您也可以选择不使用“新”部分,而仅在其“新”视图中显示用于创建新费用的表单,以使其在“新”操作中运行代码。
希望有帮助!