Rails 6 create order confirmation before submit
tl/dr: You need to add parameters to your create request.
Why this is happening
The /confirm
view is being rendered with an (unsaved) @cash_transaction
object, however that object is not being used and so the information is being lost when the page is rendered.
The line:
<%= link_to 'Confim', cash_transactions_withdrawals_path(@cash_transaction), method: :post %>
Will submit a POST
request with no parameters to the /cash_transactions/withdrawals#create
(because you've given it no parameters to post). It doesn't know to include the params from the previous request.
There are a few options to fix this... you can add params as URL parameters in link_to like this, however I wouldn't recommend posting with params in the URL.
You can use button_to
instead, and pass in the cash_transaction
arguments from the previous request in the params:
option (or pull them out of the unsaved @cash_transaction object).
Approach #1 - reuse create params
# Get them from the params sent in the previous request. In the controller...
def create
@cash_transaction = CashTransaction.create!(cash_transaction_params)
# etc...
end
#...
protected
def cash_transaction_params
params[:cash_transaction].permit(:amount, :whatever)
end
helper_method :cash_transaction_params
# In the view
<%= button_to 'Confirm', {action: 'create', params: cash_transaction_params}
Approach #2 - Access attributes from the model you built
<%= button_to 'Confirm', {action: 'create', params: @cash_transaction.attributes.slice('amount', 'other_attribute') }
Or you could do something like render the form again but hidden and have the "confirm" button submit the hidden form (with { action: :create }
instead of { action: :confirm}
). This last solution is probably the easiest to understand.
How to show the content of the form just entered in the :confirm = option on Ruby on Rails
Given that your loading attachments it may not be a bad idea to render a staging view including information derived from the attachment allowing the user to confirm. As in display the file if it's an image, or the first paragraph of text if it's a text file, etc.
It's going to take more work than the just adding a confirm pop up, but I feel the enhanced user experience is worth the extra effort.
I'm not familiar with the way that paperclip works. So you're mostly on your own for the intimate details.
You will probably have to create a record before the staging view can be rendered with the sample of the uploaded file. To accomplish that I'd set up an "active" column on the model in question, that defaults to false.
Usage would look something like this:
- User complets new form.
- Attachment is updated and records are saved, with the active field set to false.
- Redirected to confirmation page that is essentially the show page with a confirm link/button and a cancel link/button
a. When the confirm link/button is clicked it sends a request to the controller triggering the update action on this record setting active to true.
b. When the cancel link/button is clicked it sends a request to the controller trigering the destroy action on this record.
All that's left is to set up a recurring task to remove objects that are inactive and were crated long enough ago that it's safe to assume the user has just ended the browser session.
Rails form - step between new and create
1 - You could store the object in the session until the confirmation page.
In your form :
<% form_for Order.new, url: save_session_path do |f| %>
In your controller :
def save_session
session[:order] = params[:order]
redirect_to confirmation_path
end
def confirmation
@order = Order.new(session[:order])
end
2 - Pass the object through the controller actions
In your form :
<% form_for Order.new, url: confirmation_path do |f| %>
in your controller :
def confirmation
@order = Order.new(params[:order])
end
Order confirmation page in rails
There were a few answers on this
question that got me halfway there,
but the problem was that I wasn't
quite sure how to set up the form in
the rails view so that it would take
the user to a confirmation page with
all their details instead of a create
action.
Directing a form to a non standard page is pretty simple.
Add a url option form_for.
Such that
<% form_for :order do |f| %>
becomes
<% form_for :order :url => {:action => "confirm"} do |f| %>
You'll need to crate the confirm action in your routes, but that only involves this:
map.resources :orders, :collection => {:confirm => :get}
All you need now is a basic controller action and a view:
def confirm
@order = Order.new(params[:order])
unless @order.valid?
render :action => :new
else
end
end
Your view should look almost identical to the show view, with the addition of a form submitting @order to the create action.
Rails- customize view of confirm dialog box before delete
You can make use of bootstrap modal dialog box, like this:
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4
class="modal-title" id="myModalLabel">Delete Confirmation</h4>
</div>
<div class="modal-body">
Are you sure about closing Ellen's lightbox..
<%= link_to "Yes", light_path(id: light), method: :delete %>/
<%= link_to "No", "javascript:void(0)",'data-dismiss': "modal" %>
</div>
</div>
</div>
</div>
And change your initial link to:
<%= link_to "close #{user.name}'s light", "javascript:void(0)", 'data-target': "#myModal", 'data-toggle': "modal" %>
I hope this might be helpful to you.
Confirmation message - Javascript rails application
Since you already have a create.js.erb
file, you can use JS to display an alert message or insert a confirmation message into the page like so,
# app/views/.../create.js.erb
$('#new_proposition').before('<p>Proposition correctly submitted!</p>');
$('#new_proposition').fadeOut(1000);
This inserts the <p>Proposition correctly submitted!</p>
before the #new_proposition element. You may want to insert it somewhere else since this element will disappear.
Related Topics
Inserting an Array into Every Nth Element of Another
Share Session Between Two Rails4 Applications
Confused with Ruby Accessor Methods
How to Turn Off Bigint Primary Keys in Rails 5.1
Rescuing "Command Not Found" for Io::Popen
How to Make Empty Tags Self-Closing with Nokogiri
How to Combine Multiple Arrays of the Same Size in Ruby
Interpolating Regexes into Another Regex
Integrate Shoes into Aptana Studio Radrails
Permanently Switching User in Capistrano 3 (Separate Authorization & Deploy)
Ruby Code for Modifying Outer Quotes on Strings
How to Refer to a Submodule's "Full Path" in Ruby
Why Non-Explicit Splat Param Plus Default Param Is Wrong Syntax for Method Definition in Ruby 1.9