Ruby缺少模板

时间:2015-06-29 15:24:04

标签: ruby-on-rails ruby

我的控制器中有一个功能,我会渲染到另一个名为thankyou的页面。渲染工作正常,但在那之后rails再次将我重定向到thankyou,但这次它找不到模板。我无法理解为什么rails会将两次渲染给同一个模板,结果不同。这是我的功能:

def create
amount = params[:amount]
nonce = params[:payment_method_nonce]
if nonce.nil?
  render :checkout
else
  result = Braintree::Transaction.sale(
    amount: amount,
    payment_method_nonce: nonce
  )
  session[:user_id] = nil
  render :thankyou
end
end

这是调用动作创建的表单:

<%= form_tag '/create', remote: true, id: "form", method: "post" do %>
 <div class="col-md-12 text-left">
  Choose you plan: <br>
  <div>
  <input class="collection_radio_buttons" id="plan_pro" name="amount" type="radio" value="0.99">
  <label for="plan_pro">
  <span class="fa-stack"><i class="fa fa-circle-o fa-stack-1x"></i><i class="fa fa-circle fa-stack-1x"></i></span>
  <b>Pro</b><br>
  </label>
  </div>
  <div>
  <input class="collection_radio_buttons" id="plan_premium" name="amount" type="radio" value="1.98" checked="checked">
  <label for="plan_premium">
  <span class="fa-stack"><i class="fa fa-circle-o fa-stack-1x"></i><i class="fa fa-circle fa-stack-1x"></i></span>
  <b>Premium</b><br>
  </label>
  </div>
  <div>
   <div id="paypal-container"> </div>
  <input data-braintree-name="number" placeholder="Card number" class="string optional form-control input-box mailcode-form" maxlenght="16">
  <input data-braintree-name="cvv" placeholder="CVV" class="string optional form-control input-box mailcode-form" width="3" maxlenght="3">
  <input data-braintree-name="expiration_date" placeholder="Expiration date (e.g. 10/20)" class="string optional form-control input-box mailcode-form">
 </div>
  <div class="col-md-12 pull-right">
     <input type="submit" class="signup-next" id="button_1" value="Pay 0,99 €">
     <input type="submit" class="signup-next" id="button_2" value="Pay 1,98 €">
  </div>
<% end %>

PS:从日志中看来,rails称为action创建了两次。这是我提交表单后的实际日志:

    Started POST "/create" for 127.0.0.1 at 2015-06-30 15:47:06 +0200
Processing by HomeController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"S2LG9Bg4TrbXOoU4ewDBXHr0J1xtawrXy9hdSVjCBzA=", "amount"=>"1.98", "payment_method_nonce"=>""}
  Rendered layouts/_social.html.erb (0.0ms)
  Rendered home/_thankyou.html.erb (2.4ms)
  Rendered home/thankyou.js (4.3ms)
Completed 200 OK in 16127ms (Views: 8.4ms)
[Braintree] [30/Jun/2015 13:47:38 UTC] POST /transactions 422


Started POST "/create" for 127.0.0.1 at 2015-06-30 15:47:22 +0200
Processing by HomeController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"S2LG9Bg4TrbXOoU4ewDBXHr0J1xtawrXy9hdSVjCBzA=", "amount"=>"1.98", "payment_method_nonce"=>"695c94a3-d219-44b6-af6f-cfec5dbb445f"}
Completed 500 Internal Server Error in 16376ms

ActionView::MissingTemplate (Missing template home/thankyou, application/thankyou with {:locale=>[:it, :en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in:
  * "/home/marco/Scrivania/mailcoding_web/app/views"
  * "/home/marco/.rvm/gems/ruby-2.2.1/gems/kaminari-0.16.3/app/views"
):
  app/controllers/home_controller.rb:117:in `create'


  Rendered /home/marco/.rvm/gems/ruby-2.2.1/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/missing_template.erb within rescues/layout (0.5ms)

4 个答案:

答案 0 :(得分:1)

在你的app / views / home / thankyou.js.erb文件中试试:

$('#signup-modal .modal-body').html("<%= j render_to_string('thankyou') %>");

:create行动中:

render js: :thankyou

答案 1 :(得分:0)

尝试渲染thanyou模板,如下所示:

<br/>

答案 2 :(得分:0)

The method is simply called twice. You have to check why on the client side. You will probably see two calls in the developer console of your browser.

答案 3 :(得分:0)

我已修复删除 remote:true 的问题。现在我只有一个电话(HTML)。我还删除了部分 _checkout.html.erb ,我已替换为名为 checkout.html.erb 的文件