2015-06-29 72 views
0

我在我的控制器中有一個函數,我會渲染到另一個頁面,名爲thankyou。渲染工作正常,但在此之後,導軌再次將我重新導向謝謝,但這次它無法找到模板。我無法理解爲什麼rails會使我對同一個模板渲染兩次,並得到不同的結果。這是我的函數:Ruby缺失模板

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:從日誌似乎軌稱爲動作創造了兩次。這是我所提交表單後的實際日誌:

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

我可以看到,有在片段缺少'end'文件替換。除此之外還有什麼可以讓':create'做更多的事情? –

+0

不,這是我的錯誤 –

+0

我編輯了我的答案。我想當你點擊:創建並渲染你的代碼選擇的模板時,你就會顯示:創建,也許?在if後面試試'return'? –

回答

0

嘗試渲染thanyou模板,像這樣:

def create 
    amount = params[:amount] 
    nonce = params[:payment_method_nonce] 
    render action: :checkout and return unless nonce 
    result = Braintree::Transaction.sale(
    amount: amount, 
    payment_method_nonce: nonce 
    ) 
    render :thankyou 
end 
1

嘗試,在您的應用程序/視圖/家/ thankyou.js.erb文件:

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

而在你:create行動:

render js: :thankyou 
+0

我嘗試使用if塊,現在rails只渲染一次,但模板仍然丟失。我在另一個函數中有相同的渲染調用,並且它工作正常 –

+1

實際上,在控制器中是否有一個名爲thankyou的動作,或者只是一個名爲thankyou.html.erb的模板? –

+1

我有一個部分叫_thankyou.html.erb –

0

該方法簡單地稱爲兩次。 你必須檢查客戶端的原因。 您可能會在瀏覽器的開發人員控制檯中看到兩個調用。

+0

你是對的。我看到兩個電話。第一個是xhr類型,第二個是文檔。我只有第二個 –

+0

那麼你需要分享我們的頁面代碼。錯誤在那裏。 – coorasse

+0

我粘貼了調用函數create的表單的代碼 –

0

我已修復,刪除遠程:真。現在我只有一個電話(HTML)。我也刪除了部分_checkout.html.erb和我有一個叫做checkout.html.erb