Automatic non-RESTful routes in Rails 3? - ruby-on-rails-3

We have an app with a large number of non-RESTful, verb-oriented controllers. I remember that long ago, before Rails got the REST religion, you could get automatic routes and helpers for those. Is there any way to still do this? It's a pain to keep adding GETs every time we add an action, and our app (or perhaps our collective development brain) just doesn't lend itself to RESTfulness.

You can use the "magic route", I believe it's still in the routes file by default, but if you don't have it here it is:
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id(.:format)))'

You can still use a default route like this:
match ':controller(/:action(/:id))'
to match paths like
/monkey/play
/monkey/see/1
/monkey/hear/1
/monkey/eat/1

Related

Dynamic routes but not in the traditional sense

I wanted to know if there is a way to dynamically generate routes based on data from a database?
Currently, i am defining my routes in a routes file and then importing that into my vue project. Is there a way i can have specific configurations stored on a database such as the path, name, meta data and then when the application loads, depending on the auth level of the user, create routes for that user?
Reason why I'm asking to create and not use a pre-written route with params is because i want to give my users (at some point in the future) the ability to create their own pages from my system.
So just wanted to know from the community if there is a way to do this based on an axios call or something?
You can just use dynamic routing. To create new templates, code must be changed anyway.
I think technically you are still better off using a title parameter with a common prefix and just looking up that title. In theory it sounds nice to have a completely dynamic application where anyone can create any page... until someone decides to use "login" as the page name and override your own login component, making the app unusable.
That said, you can use router.addRoutes to dynamically add routes to your router. Create a router with the static routes (e.g. your homepage, your login page, your 404 page), then extend your router based on an api call.

Changing a namespaced admin route into a root route

I have my Pages model, views, and controller in the Admin namespace. However, I would like to display the pages at a root level. Instead of admin/pages/[:id], I would like the routes for showing pages to be /[:id]. Is it possible to do this without creating multiple controllers and models for Pages and while still keeping Pages in the admin namespace? If so, what is the best approach and how would you have to write your routes?
As far as I know, and as far as I've tried, I don't think theres a way to change the routes for showing pages to be /:id instead of admin/pages/[:id] without creating multiple controllers for Pages and while still keeping Pages in the admin namespace. Referring "without creating multiple controllers and models for Pages and while still keeping Pages in the admin namespace", I don't understand creating a new model. Creating a new controller will suffice.
For a non-nested resource, changing the route to a root route is relatively an easy task, i.e. say /:id instead of foo/:id
p.s. initially, I did not give your question a deep thought, and skipped the admin namespace part. But, trying this turned out to be fun. I'd really want to see someone post an answer to this (this is one answer where I'd love to be downvoted and pointed towards the correct answer).

is this a good place to DRY out code?

The owner of a site that I am working on has asked me to make the About Us page editable (by her, through a web interface). In fact, there are 5 pages in total that she wants to make editable - About Us, Terms of Service, and so on.
In the old implementation, when these pages were static view files, I had all the URLs coded into routes.rb
scope :controller => :home do
get :about
get :terms
# etc ...
end
Now that these different actions are fetching data from the DB (or wherever) it seems like the standard RESTful approach might be to make a Pages resource and consolidate all the views into a show action.
That doesn't feel quite right. Individual resources aren't usually hardwired into the site the way an About Us page is - the contents of the page might change, but the page itself isn't going anywhere, and there are links to it in the footer, in some of our emails, etc.
Specifically, factoring out the individual routes from the PagesController would raise the following problems:
I couldn't used named route helpers like about_path
The routes for permanent pages on the site would be stored in the database, which means that...
maintenance would probably be a headache, since that is not the normal place to keep routes.
So currently I think that the best approach is to leave these URLs coded into routes.rb, and have separate controller actions, each of which would fetch its own page from the DB.
Can anyone share some insight? How do you deal with data that's not totally static but still needs to be hard-wired into the site?
If you are going to allow markdown, I like the idea of a Pages controller and model. If your layout feels like all 5 pages should have a similar feel, then I'd go with one template that populates with the user generated content and appropriate navigation.
My choice would be to set the routes, make the views (including routing), and populate the views with the user generated markdown.
Without knowing more about your site, it's hard to say, but my preference is not to allow users to generate pages that reflect the site identity (About, terms, etc.) unless that's what they are paying for.

Rails 3: routing to customer areas

I'm a Rails 3 beginner, but have experiences with other MVC web frameworks and need a starting hint about how to setup my routing in Rails. The application should allow users to register and after that the users data should be available at URLs like:
http://domainname/username/xyz
The common and user independent part should be available at
http://domainname/abc
To distinguis between both routes, I would force usernames to have at least 6 characters and all "abc"-routes will have 5 or less. Until this point I would be manage the routing by myself, but for the "xyz" part of the user area I would like to use the existing REST full features of rails. Any hint how to do that?
Have you already read http://edgeguides.rubyonrails.org/routing.html?
It's really a good resource for this sort of question. I believe some of the scheme you describe falls into the "Non-Resourceful" routing category.

Rails 3 routing

Actually there is any easy way to prepend '/#!/' name prefix to all routes in rails 3?
like twitter and facebook
http://twitter.com/#!/username
I need that to manage ajax request.
Thanx very much
I doubt there's a built-in way to achieve it, though it wouldn't be impossible to write a JavaScript helper to turn to the server for new requests with #!/ left out from the URLs, and catch all click events to turn them into URL hashes.