Note that there are some explanatory texts on larger screens.

plurals
  1. POForm to select id before passing it to 'new' form in Rails
    primarykey
    data
    text
    <p>I have some two nested resources, so that a Product can have many Bookings. On one page in a different controller I want to create a new booking and since it's a "general" booking I want a select menu to appear in a modal window where the user is able to pick one of the existing products and then go to the booking page.</p> <p>How can I do this?</p> <p>I have no problem setting up the modal and the "New booking" page is already there. What I need is a form that generates a list of existing products, picks the selected product id and then gets /products/:product_id/bookings/new .</p> <p>To clearify;</p> <p>I have the two resources set in routes:</p> <pre><code>resources :products do resources :bookings end </code></pre> <p>A product has many bookings, and thus the path to create a new booking will be /products/:product_id/bookings/new.</p> <p>I have another controller with a view where I want a button for creating a new booking. Problem is I don't know for what product the user wants to create a booking when the "New booking" button is clicked. To solve that my plan was to show a modal window containing a select with all products, that the user should be able to select a product and then be redirected to /products/:product_id/bookings/new with :product_id as selected product.</p> <p>Right now I have a solution that works but it's not very nice, I have created the form with an action to /products/:product_id/bookings/new and with method = get. Then I catch the submit with JavaScript, change the action and then submits again:</p> <pre><code>$("#new_booking_form").submit(function() { var product_id = $(this).find('select').val(); var form_action = $(this).attr("action"); var new_form_action = form_action.replace(/product_id/, product_id); $(this).attr("action", new_form_action); $(this).submit(); }); </code></pre> <p>This solution works but is very ugly! Any suggestions?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload