t56k
t56k

Reputation: 6981

Rails: passing variables in URLs

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!

Upvotes: 1

Views: 1306

Answers (1)

user229044
user229044

Reputation: 239250

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.

Upvotes: 7

Related Questions