Skip to main content

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

Rails • Asked by Joel
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

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 22,346+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.