All threads / How do I preselect a reference to a resource that is not a parent in the routes?

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I preselect a reference to a resource that is not a parent in the routes?

Joel asked in Rails
I have an app with a ContentCategory model that has_many :contents.  In the ContentCategoriesController#show view there is a button for "New Content".  This goes to the ContentsController#new view which has a select box for the content_category.  Currently this defaults to the first category in the list instead of the desired behavior of defaulting to the ContentCategory of the page before.  Is there a way to achieve this without nesting the resources in routes.rb?
 
Hey Joel, 

Is the name of the category in the url of the previous page? 

If so, then 

request.referrer

will give you the url of the page that sent you there.

Otherwise, you can just pass it as a param that would appear at the end of your url ?cat=meow
Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 35,699+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.