1

I'm trying to access a resource in my rails app that I want to populate with information based on the variable in the URL. Given the URL:

someapp.com/resources/variable/resource

I would like to pass the variable to Rails to add elements to resource. Is there any quick re-routing way of achieving this? Or is it insanely complicated?

Cheers!

1 Answer 1

7

No part of this should be "insanely complicated", but it's not very clear what you're trying to accomplish.

You could try adding a route with a variable segment:

get "/resources/:variable/resource" => "controller#action"

Then, in your controller, access params[:variable].


If your already have a resource defined, you can add an additional "member" route via the following:

resource :resources do
  get :resource, on: member

  # or

  member do
    get :resource
  end
end

Either of the previous will allow your controller to route /resources/:resource_id/resource to the resources controller's resource action.

Sign up to request clarification or add additional context in comments.

3 Comments

That's giving me clues, yeah. But if the resources/resource already exist it doesn't seem to work. In my routes.rb I have already resources :resources.
See my update. Also, resources is a very bad name for a resource. Your resources should have specific names, matching the things they represent.
Ah, that helped me so much! Cheers. And my resources aren't named that, I just used it for the example. Which ended up being far more complicated than it should have.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.