0

I have the below code for creating a product:

def create
    @customer = Customer.find(params[:customer_id])
    @product = @customer.products.build(params[:product])

    respond_to do |format|
      if @product.save
        format.html { redirect_to customer_products_path(@customer), notice: 'Product was successfully created.' }
      else
        format.html { render action: "new" }
        format.json { render json: customer_products_path.errors, status: :unprocessable_entity }
      end
    end
  end

To generate the form I use:

<%= form_for([@customer, @product]) do |f| %>

Now, I am wondering, how can I transfer the use to a separate confirmation page before saving the data to the DB. Also with an edit link, so if needed the user can make changes and submit the records finally.

I looked at other questions on stackoverflow, but was unable to create what I want. Any guidance is appreciated.

4
  • I think it's better to resort to a client side confirmation dialog using javascript client side libraries. Commented May 20, 2013 at 13:12
  • I am not very good with JS. Can you point to some reference guide or other similar pointers? Commented May 20, 2013 at 13:16
  • 1
    JQuery UI confirmation dialog is just an option from a large pool of options. Commented May 20, 2013 at 13:22
  • you can use multi step form Commented May 20, 2013 at 13:23

2 Answers 2

1

If you want to do client-side confirmation, you need to use JavaScript to listen on the 'form submit' event. You then use jQuery html() to show a lightbox that asks to proceed or not. It would be related to using onsubmit event and rendering a confirmation box. Another way with data attributes is discussed here

If you want a database tracking for confirmation (server-side), you can render your model in the 'show' action, but with a different set of buttons.

In your show view, you would then do:

<% if @product.confirmed? %>
    ... # normal path to actions for show (edit, delete, back )
<% else %>
    <%= link_to "Confirm', confirm_product_customer_path(@product, @customer) %>
<% end %>

However, having an attribute for confirmation in the database, sometimes is a bit of overkill. As Jani suggests in the comments, a client-side confirmation is for most cases good enough.

Sign up to request clarification or add additional context in comments.

4 Comments

If its going to the show method the record is getting created. What if the user doesnt want to proceed after the confirmation page is rendered?:)
Right, with this, you 'create' the model, and then 'confirm' the model. It's is sometimes necessary when a user makes a transaction. If you don't want to write to the database, you need client-side confirmation, e.g. with a lightbox.
Ok, I added some links on client-side rendering. Is this answer better?
Yes. Although i am gonna tweak it fit my needs. Get the idea though. thanks :)
1

You can use a new button with a parameter:

  <%= f.submit %>
  <%= f.submit "Preview", :name => 'preview' %>

and in your controller, in create action:

if params[:preview]
@product = Product.new(params[:product])
render 'products/previw'
else
# save your object
end

and create a new partial for preview.

2 Comments

What exactly goes inside # do your stuff.?
- I create a new preview view 'products/preview.html.erb' - How I would save all date on preview view to my DB as a confirmation step?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.