All threads / How do I pass an id to a foreign controller?

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I pass an id to a foreign controller?

Mark Webster asked in Rails

A very basic question I know, but I'm really struggling. If I use a link_to with the current controller's path then I get the id as a parameter but if I instead use a path to a different controller then I get a format instead.

So from the same view I get the following paths generated by the team show view:

team/2/add/ and player/add.2

TIA

MarkW

Hi Mark,

Can you post your routes and the link_to you're using?

Hi Mark, I'm with Jacob. If you could show the code that would be great.

Also, what are you trying to do? Do you have a single page with multiple objects executing different types of CRUD functionality? If thats the case then perhaps you should use a view object that is a single model that represents all the models on that page. That way you can maintain RESTful routes and not have to try and share object id's accross controllers.

I'm sure Chris has some content around executing this kind of strategy. If not, I'm happy to share some additional context if need be.

Join the discussion

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

Join 33,665+ 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.