make rails url helpers use the appropriate protocol? - ruby-on-rails

Hey, I'm allowing my app to be accessed via https and http, and everything is working fine except for when I use any of the _url methods. If I access a view that uses such a method via https, it does get served as https, but the generated url uses the http protocol. I'm wondering if this is normal, or if there is a way to make it automatic. I was hoping rails would automatically generate the appropriate url based on how the page was accessed.
If there's no way around this, what would be the best way to create the appropriate url? Would this work?
if request.ssl?
some_url(:protocol => "https")
else
some_url
end
I would prefer it if I could come up with a more automatic approach. Maybe if the rails url methods generated ://somedomain.com/some/path, that way I imagine it would automatically adapt the correct protocol.
Thanks, I would appreciate any help.

Take a look at http://ianlotinsky.wordpress.com/2010/09/29/ssl-in-ruby-on-rails/

It looks like there are a few approaches. None of which may be ideal in your particular environment, but in the ActionView::Helpers, there is a url_for method that might get you what you want if you start using that. The other simple option is to use relative URLs (ie: '/controller/action' instead of 'https://myawesomesite.com/controller/action') which can be generated by the url_for method too.
Beyond that, it looks like you're getting in to work arounds, or digging in to the rails path generating code and altering it yourself.
Not probably the most ideal answer you were looking for, but there's my $0.02!

Related

Rails function that searches for my link

I'm making a tool for a website thats under contruction that makes linkbuilding easier. This site is written in Ruby On Rails. Now that i'm learning RoR, i don't know were to start to make a function that searches a external site, which URL has been saved, to check that my url exist on that particular site. If so => display, if not: set URL non-active
So my questions:
Can I search a site from my application?
What is the best way to do this?
How often would you make such a script run?
It sounds like you are trying to verify a URL is active.
Answers to your questions:
Yes you can make HTTP requests to another URL.
For making HTTP requests look at the Nestful gem or you can use Net::HTTP from the standard lib.
You don't give clear information about your requirements, so it is hard to say what is right, but you could run the check when the author submits the form. If you worry about slow responses use delayed_job to make the check asynchronous.

Rails 3 respond_to :html and preventing double content

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.

Rails or web2py style URL routing in Django

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.

Is it possible to do AJAX calls in a liquid template?

I'm looking at the liquid templating language for Rails apps:
http://wiki.github.com/tobi/liquid/
I'd like my users to also be able to make AJAX calls (just like the ones in rails for periodically_call_remote, observe_field, etc). Is this possible?
Assuming the rails helpers can be added as filters, how will the user be able to modify what gets returned by the AJAX call? They cannot modify an rjs file on the server or anything like that. I suppose the AJAX call could return JSON (instead of rendered html) and then the javascript could use that to render something. But I'm having a little trouble envisioning how it would work exactly.
If anyone can point me to an example of this or clarify it'd be much appreciated. Thanks!
Is allowing any user to make any ajax call really what you want ?
Don't forget you can't trust your users. Do you really want them to be able to request any page on your domain name ?
I guess you want to be able to allow them to request some pages only though. A defined list of urls.
Then you can just create one filter per url that'll return your content.
So if the user does in his template :
{{ get_users }}
Which will do, internaly, an ajax call retrieving the list of all your users.
You can think generic when you're working on things that only developers should be working on.
But when it's about allowing your users to change the code of your application, you should restrain them so they don't hack and break everything.

What makes an effective URL Mapping implementation and why?

I am looking at implementing URLMapping for a personal project. I am already aware that solutions exist so please do not answer suggesting I should use one.
What I want is to harvest the opinions of fellow developers and web users on URL mapping implementations. Specially I would like you to answer:
Which is your favourite implementation?
What do you like about your favourite implementation?
What do you not like about your favourite implementation?
How would you improve it?
I would like you to answer from two points of view:
As a developer
As a user
I would be grateful for any opinions on this matter, thanks!
I've only worked with django's URLConf mechanism. I think the way it relies on the urlpatterns variable is a bit flimsy, but I like its expressiveness in specifying patterns and dispatching to other url configurations. I think probably the best thing is to figure out your URL scheme first, and then try out a couple of solutions to see what matches best. If you're going hard-core REST using the full complement of GET/POST/PUT/DELETE, and checking the user agent's Accept headers, and all that, django will, by default, have you splitting your logic between URL config files and view files, so it might not be the cleanest solution.
However, since it's all Python, you might be able to do some more complex processing before you assign to urlpatterns.
Really, you want a system that does what you need. Your URL scheme is your API, so don't compromise on it based on the tools you use. Figure out your API, then find the tools that will let you do that and get out of your way.
Edit: Also do a google search for "URL scheme design." I found this without much effort: http://www.gaffneyware.com/urldesign.htm. Not comprehensive, but some good advice gotten from looking at what flickr does.
Well, I should have noticed the url-routing tag shouldn't I? :-) Sorry about that.
jcd's experience mimics mine - Django is what I use too.
I like the fact that the routes for an app reside within the app (in urls.py) and can just be included into any projects that might make use of that app. I am comfortable with regular expressions, so having the routes be specified in regex doesn't phase me (but I've seen other programmers scratch their heads at some more uncommon expressions).
Being able to reverse a route via some identifier (in Django's case by route's name) is a must. Hardcoding urls in templates or controllers (view in Django) is a big no-no. Django has a template tag that uses the reverse() method
The one thing I wish I could do is have the concept of default routes in django (like Rails does or even Pylons). In Django every route has to map to a view method, there is no concept of trying to call a certain view based on the URL itself. The benefit is that there are no surprises - your urls.py is the Table of Contents for your project or app. The disadvantage is that urls.py tend to be longer.

Resources