custom routing in rails - ruby-on-rails

I have a rails application that has a model named graphic and as any rails application there is normal routing like this
something.com/graphics/1
something.com/graphics/2
something.com/graphics/3
which will take you to the appropriate show pages. That I understand this is done in my routes by this statement
resources :graphics
Now come to find out the client wants to have the url to be like this
something.com/1
something.com/2
something.com/3
so if there is a number directly after the root url then like it to the graphic show action....any ideas on how to do this without messing up any other models

Seems like you are looking for:
match "/:id" => "graphics#show"
You can refer to http://guides.rubyonrails.org/routing.html for any further modifications.

Related

User defined routes in Rails

There is a lot of good information on routing in Rails. I must be missing something, but I can't seem to find a good example of a Rails application that allows dynamically defined user specific routes.
For example, my application is hosted at:
www.thing.com
... and serves out user generated content.
I'd like to give the user an option to define a suffix that let's them share a somewhat customized URL to their content. For example, if a user 'joe' generates some car info they might want to make it avilable via joescars at:
www.thing.com/joescars
Maybe later they decide they want to serve it out under 'carsbyjoe' at:
www.thing.com/carsbyjoe
I can handle limiting what suffixs are valid. Is there a Rails way to codify this kind of dynamic routing?
There is a way to do this. In your config/routes file add a route that says get '/:user_route' => 'somecontroller#someaction'. You'll have to put it at the very bottom because routes are matched from top to bottom and this will match things like /users or other routes you'll likely want directed elsewhere.
Then, in your controller you can access params[:user_route] to show the appropriate content. There are a number of ways to store this custom content in your database, depending on your needs. You might have a model representing these custom routes like CustomRoute.find_by_route(params[:user_route]), or maybe each user will have a custom route so you could do User.find_by_route(params[:user_route]).custom_page and each User has one custom_page.

Ruby on Rails database driven router to support fully customizable urls

I'm planning to port our current cms (written in PHP) to Rails. All parts do well, except for one: routing.
Like most cms systems, the routing in our cms based on a database with pages, which are linked to modules, controllers and actions. On this approach a user can fully customize or specify it's own urls.
I know that Rails (and most (application) frameworks have the approach of defining routes in a file, but I hope this is possible.
The approach our users should have is:
add new page
select type (news, form, product, ...)
select an item (select which form, blog or product should be displayed)
enter a url for that page
Special the last point (4) is important. A user should be able to add form A to /contact-us, and form B to /clients/register-as-new-client e.g.
On a request the router needs to do a database query with the page url, to find out which controller, task and parameters should be dispatched.
Question has been updated, and i don't think this is a valid answer anymore
we have a similar paging system. we use a routing glob. in routes.rb:
get 'pages/*lookup_path', to: 'pages#show', defaults: { format: 'html' }, as: 'page'
Just parse params[:lookup_path] in PagesController to suit your needs
'http://localhost/pages/users/'
params[:lookup_path] #=> users/
'http://localhost/pages/users/23'
params[:lookup_path] #=> users/23
'http://localhost/pages/people/1'
params[:lookup_path] #=> people/1
Although this solution isn't ReSTful, I think this should solve the issue.
Regardless, Rails uses routes in a file. You cannot change this since the framework heralds "convention over configiuration". All I can do is point you in a direction to minimize this.
There is a catchall route in Rails (on RailsCasts, and on StackOverflow) which you can use to direct all routing to one controller action. You may further customize the routing behaviour in that method
You could also make a route like…
:controller/:action => Controller::Action
…as is done in CodeIgniter, but now your methods have to have names like contact-us and register-as-a-new-client.

Resolve Route Server Side in Rails

Just as you figure out the route when the browser hits the webpage in Rails, how would you resolve it on the server side?
For example I want to return a URL to a RESTful resource called Bookmark in an API call and want to return the 'show' action of it, and I know that:
Bookmark id: 12
Then I want to resolve it to a string:
'/bookmarks/edit/12'
so that I can get this from my Model for example.
How would I go about doing this?
Thanks!
Pretty much everywhere in the views/controllers you can use route helpers to DRY up route references.
In models, you'll need to explicitly call the route helper like so.
Rails.application.routes.url_helpers.edit_bookmark_path(id) # => '/bookmarks/12/edit'
When using the default resourceful route generator method in routes.rb like
resource :bookmarks
I'm not sure I understand - your server is the thing that's making all of those routes work - the client's browser isn't figuring out what the route is - you application is doing it.
The paths to your resources are available as helper methods at all times (at least within the controllers, and views). As such, you should return the string as the body of a response in one of your actions, in the controller that's handling your API calls.
Check your rake routes on the command line, and you'll see a list of them. In the case of your example above, it would likely be called edit_bookmark_path(12)

Custom Nested Routing in Rails 2.3

I'm trying to change some URLs in this project I'm working on, but I can't find any examples of what I'm looking for in any documentation and all my attempts seem to end up not working.
Here's what I'm trying to do. I have a post that has several images, and an individual image would be accessed with an URL like this:
/posts/aliased_title/images/2
However, I want to change it so I can remove the "posts" and "images" part of the URL, change it to something like /aliased_title/2.
I know how to do this for a single controller, but how can I accomplish this with the images controller as well? Can I do this in a way that will generate the "paths" (like post_image_path), or do I need to hard code every single action into the routing?
Thank you in advance.
Depending what your routes and aliased titles look like, you can use something like the following:
resources :images, :path => '/:title/:id', :constrain => { :title => /[a-z_-]+/ }
# in your view
image_path(image.title, image)
Keep in mind that routes match in the order they are defined, so you will want to put this line down at the bottom of your routes file so that it doesn't interfere with routes like /users/2.
All this said, you should strongly consider whether your use case can live with an anchor like /image. It'll prevent a lot of confusing problems with routing that will very likely bite you in the ass down the line. It's not infeasible to do without the anchor, but it does create additional work.

Root-level routing with Ruby on Rails?

How do I efficiently enable root-level routing with Ruby on Rails?
For example, instead of having:
/questions/a-question-here
I want:
/a-question-here
What technology would I use, and how would I configure the routing to enable this?
EDIT: I also have other models such as videos and users, so I'd like the routing to match other controllers as well. For example, how do I get /username to go to some action in the Users controller and /some-video-title to go to some action in the Videos controller?
Thanks for all your help, people.
Rails has a very configurable routing layer built in. Good documentation here:
http://guides.rubyonrails.org/routing.html
Your route could end up looking something like this:
match ":id" => "questions#show"
Depending on where you put that, it could override a lot of other routes, so be aware of that.

Resources