We're building a custom cms and I was wondering if it's possible to have everything go through one route but to conditionally show a different view. So for example, everything is an asset but the sub-type could be article, or video, etc. Would it be possible to conditionally map to an article view?
You can render whatever you want. (Nutshell version.)
It sort of sounds like some sort of class inheritance should have been used, though, and each should have their own controller/templates.
This is not really a good idea, if I understand correctly. Sounds like you're trying to reinvent Rails' routing system. Instead of reinventing it, use it!
If I misunderstand, please explain your use case in more detail.
Related
I'm trying to learn a bit more about creating friendly URLs, particularly in relation to something like an e-commerce site. If you take a look at this site as an example: http://www.wiggle.co.uk/
It basically lets you refine your search based on the categories. So if I go to http://www.wiggle.co.uk/mens/road-time-trial-bikes/ it will list all men's road bikes, if I then go to http://www.wiggle.co.uk/road-time-trial-bikes/ it will list men's and women's bikes, and if I go to http://www.wiggle.co.uk/bianchi-sempre-105/ it will display a particular bike.
I'm not really sure what the routing would look like for something like this, as you could have many filters in the URL. I'm also not sure how how it's able to distinguish between what's a filter and what's a product.
I'd say you are fairly close but would break out the second and third as it would be a little ambiguous for the one Action to interpret the two parameters. I'd suggest the following:
http://www.wiggle.co.uk/road-time-trial-bikes/
http://www.wiggle.co.uk/bikes/bianchi-sempre-105/
Thus /bikes/ could be routed clearly to a different controller and action. You would need to create a specific rule for the root or you could add a specific controller, say bike-types, to the first url
http://www.wiggle.co.uk/bike-types/road-time-trial-bikes/
The specifics on routing are well documented but a good place to start might be with writing some tests to help you understand what works and give you more confidence with making changes to your routes. It is very easy to create clashing routes and tests help prevent this.
http://mvccontrib.codeplex.com/wikipage?title=TestHelper#Routes
I'm trying to prevent double content in combination with respond_to. So I added to my controller:
respond_to :html
In combination with respond_with this works great so far, butt he user can still access my pages using either:
/my/page
/my/page/ or
/my/page.html
I'd like to limit this to one of them (preferring the first one). Removing formats fully is not a solution, because I might like to respond_with json or xml someday.
Thank you in advance for any tip!
Doing this will be pretty unpleasant and will also make your site more difficult for users to, well, use. Unless there's a really, really compelling reason for you to do this, I would just work with the Rails defaults and accept the fact that you have a lot of different URLs pointing to one resource. Doing so really shouldn't hurt you.
Again, you should probably not do this. But if you really wanted to, you could play with routes.rb to manually create the routes you wanted. Check out the Rails routing guide for more information on how to generate Rails routes that look more like what you want.
That said, that still probably won't be enough to get rid of my/page/ and my/page pointing to the same place. If that is really, truly necessary, I would consider dropping Rails entirely and using a different framework like Sinatra, where you have very fine-grained control over routing (since the framework makes no routing assumptions at all).
But the best thing to do is probably just accept the routes as they are and move on.
I'm new to Django. Seems that Django requires to define one URL mapping rule for each controller/action (view/function in Django's term). What's the easiest way to implement Rails-style URL routing, or at least web2py-style URL routing?
For those who don't know what is Rails or web2py style URL routing.
Rails RESTful URL routing
web2py URL routing: http://domain.com/C/A maps to C controller A function automatically.
What you're trying to do goes explicitly against Django's design philosophy: "Tying URLs to Python function names is a Bad And Ugly Thing." So I think you have two easy choices and one hard one.
Easy #1: Stick with rails
Easy #2: Use django the way django is normally used: one URL rule per view.
Difficult: Write a generic URL dispatcher that uses introspection to look up view methods based on URLs. Be very careful making it secure. And share it when you're done. :)
Not saying you shouldn't do it. But if you're looking for an easy way to do this I suspect you'll be frustrated.
You can write it easily in custom way - write one view which accepts the url and parses it. then it loads module you want. Then you will have only one rule in urls.py file, which will call just one view function.
Also you can create middleware probably to solve the issue you have....
But I wouldn't recommend to do so and I think it is best to stick with the style of routing Django uses. Why? Because later then you will have to think about custom url resolvers, you will not be able to use for example {% url %} template tag (while you will not customize it also). I think it is not worth to reinvent something here... But this is only my opinion, it is you who decides :)
Have fun :)
I would recommend looking over the documentation for the Django URL dispatcher. There you should a bunch of ways to solve your problem. However if you are looking for a quick answer on a faster way to define your URL's then I would take a look at View Prefixes they cut down some of the bloat that you may be seeing.
If you are looking at REST then I would recommend taking a look at this article.
I have a customer that wants to build their own questionnaires. Something like WuFoo (www.wufoo.com) but more secure and contained within the app.
I've looked at Smerf (http://github.com/springbok/smerf) which provides the yaml-to-form conversion, but I'd like something the user can use to create their own forms.
I would look at using active_scaffold. The main version has not been updated for Rails 3, but a fork at the location below has. I think it would work well for your purpose, you just need a way to grab the data and feed it in. Here is a demo of what it looks like when it is running:
https://github.com/vhochstein/active_scaffold
Here is a demo at the top of the page:
http://demo.activescaffold.com/roles
You could always embed Google Forms. Might be easier than reinventing the wheel. Unless you have some specific use case this doesn't cover?
If you are not adverse to going the Javascript route the then you might consider one of the many framework plugins like the JQuery Form Builder. From a usability perspective it seems to me that any good solution is going to involve some Javascript. There should be no reason why this approach wouldn't integrate well into a Rails backend
You might want to check out this one. Dynamic Forms
I too am looking for something very similar. What solution did you come up with?
I have a Rails application with several models-views-controllers which have some similar characteristics, for example 5 different models can be commented on, voted on or tagged, I am also heavily using external plugins.
At the moment I introduced comments, votes, tags, etc. only to a single model (and its view and controller). However, now that I am happy with the results, I want to cut out this common functionality from the particular MVC of one model and allow access to it from all other models.
Some questions before I start doing this (and maybe some general advice will also be great):
1 - How should I go about it? I was thinking creating a module in "lib" directory (is it the same as mixin class?) and then moving reusable view code to common partials. What about the controller code?
2 - As I was just learning Ruby on Rails during the coding of the first model, I went with a probably incorrect way of adding a bunch of methods to the controller. I have a method that adds a comment (addcomment), adds a vote (addvote), etc. All these methods require non-standard (non-RESTful) routing via :collection. From what I understand, the correct way would be to move comments controller functionality to its own controller and access via standard RESTful routes. Is this what I should be doing?
3 - Many plugins (eg. act_as_commentable) do not explicitly require loading a Module, just a line "act_as_commentable" somewhere in the Model. Can I use something like this for my common functionality? How does it work?
A simple way is to split the code into modules and use mixin.
A better way is to write your own plugins for your common code.. like act_as_commentable
you can learn about it here: http://guides.rubyonrails.org/plugins.html
The correct way is to do a comments controller, and have it nested to your models, giving a restful routes like this: /mymodelname/1/comments.
An easy way to make such controllers is by using inherited_resources plugin.
scroll down to the "Polymorphic belongs to" section- there is a comments controller example
For repeated model code, put it in a module in the lib directory.
For controller code, put your duplicate code in ApplicationController.
For your view code, use partials.