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?
 
Reply
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
Reply
Join the discussion
Create an account Log in

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

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

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