edev.io
edev.io

Reputation: 560

Redirect_to Updating Dropdown Value

I have created a simple form with some drop down values, you can also add a new value to this drop down by creating a new value on a different page. On completion this page uses the redirect_to method to return to the previous page.

if @sweet_type.save
    format.html { redirect_to session[:return_to], notice: 'Sweet type was successfully created.' }

This all works fine, but I was wondering if it is possible to highlight the newly created record in the drop down box once the redirect is complete?

Any hints or tips on this are more than welcome.

Thanks

Edit: Adding Images for perhaps more clarification.

Clicking Add a Sweet Type here

https://i.sstatic.net/KVg4r.png

Adding a new type here

https://i.sstatic.net/cC5rD.png

To then return to the previous screen with the newly created item being the 'default' value.

Upvotes: 0

Views: 197

Answers (2)

amit_saxena
amit_saxena

Reputation: 7614

You can pass the id of the newly created sweet type as a parameter to the next request using:

format.html { redirect_to session[:return_to](:sweet_type_id: @sweet_type.id), notice: 'Sweet type was successfully created.' }

Then in the view where you are rendering the select list, you can select this particular sweet type (which is in params[:sweet_type_id]) as default. Please note, that this may vary depending the way you are creating your select list. I am showing you an example. Effectively, you need to set the selected property of that option in the select list, using whatever helper you are currently using (look at the API documentation of the helper you are using).

<%= f.select :sweet_type_id, @sweet_types, :selected => params[:sweet_type_id] %>

Upvotes: 1

HungryCoder
HungryCoder

Reputation: 7616

You can pass the ID of the newly created object (@sweet_type.id) when redirecting as query string. Then in the page where you are redirected to you can highlight that using javascript/jQuery. Please check the documentation of redirect_to helper method to know what are you options for adding query string to the URL.

Highlighting may be like flashing the whole select tag or putting a border around it or whatever you wish.

Upvotes: 0

Related Questions