Reputation: 11
I have a function in my controller and i would render to another page, called thankyou. Render works fine, but after that rails redirects me again to thankyou, but this time it can't find the template. I can't understand why rails renders me two times to the same template, with differents results. This is my function:
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
This is the form that calls the action create:
<%= 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: from the log it seems that rails called action create twice. This is the actual log after i've submitted the form:
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)
Upvotes: 0
Views: 228
Reputation: 11
I have fixed that removing remote: true. Now i have only one call (HTML). I have also deleted the partial _checkout.html.erb and i have replaced with a file called checkout.html.erb
Upvotes: 0
Reputation: 3819
Try, in your app/views/home/thankyou.js.erb file:
$('#signup-modal .modal-body').html("<%= j render_to_string('thankyou') %>");
And in your :create
action:
render js: :thankyou
Upvotes: 1
Reputation: 5538
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.
Upvotes: 0
Reputation: 7146
Try rendering the thanyou template like so:
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
Upvotes: 0